Skip to content

Latest commit

 

History

History
374 lines (313 loc) · 16.2 KB

emacs-faq.org

File metadata and controls

374 lines (313 loc) · 16.2 KB

emacs-faq

保存文件时查看被修改的内容

当使用`C-x s’保存多个文件时,若Emacs提示你是否保存某个文件,可以按`d’键查看buffer与磁盘文件内存之间的差别.

如何使windows下的Emacs支持TLS

ezwinports 下载gnutls库,并释放到emacs安装目录下,重启Emacs即可.

重启Emacs后执行 M-: (gnutls-available-p) ,结果应该为t,表示Emacs已经支持TLS了.

ido

如何在不使用第三方插件的情况下,将ido的排列方式改为一行一列候选项目

默认情况下,ido将多个候选项排在一排显示,可以通过设置变量`ido-decorations’的第三个元素值或`ido-separator’为’\n’的方式,将ido的排列方式模拟成一行一列候选项的形式.

org

在org中快速跳转

org-mode提供了`org-goto’命令,可以快速定位

(define-key org-mode-map (kbd "s-u") #'org-goto)
(define-key org-mode-map (kbd "s-U") #'org-mark-ring-goto)

如何在org-table中插入`|’

\vert{} (不带等号)代替 `|’

如何在导出时,保持下划线不转义

可以在一个org的开头,使用下面的设置来关闭下划线转义功能

#+OPTIONS: ^:nil

如果需要更方便的设置,可以设置为

#+OPTIONS: ^:{}

这样当写`ab‘时,{}中的b会被转义 而直接写`a_b’则不会转义

如何在点击shell/elisp类型的link时,不要弹出确认窗口.

默认情况下,调用`org-open-at-point’打开shell/elisp link时会弹出确认窗口,太麻烦.

好在org提供了`org-confirm-shell-link-not-regexp’和`org-confirm-elisp-link-not-regexp’两个变量用于指定打开那些shell/elisp link时不用确认直接打开.

例如,下面的代码表示所有的elisp link都不需要确认就直接打开,所有以mstsc开头的shell link也不需要确认就可以直接打开.

(setq org-confirm-elisp-link-not-regexp ".")
(setq org-confirm-shell-link-not-regexp "^mstsc")

调用`org-open-at-point’还可能打开新的org shell output buffer,可以加个advise忽略它

(defun org-open-at-point-without-shell-output-buffer-advise (ori_fn &rest args)
  ""
  (save-window-excursion
    (apply ori_fn args)))

(advice-add 'org-open-at-point :around #'org-open-at-point-without-shell-output-buffer-advise)

如何让org-mode显示http url中的图片.

默认的org-display-inline-images只能显示本地图片. 要想显示http url中的图片,可以通过以下代码来实现

(defun org-display-inline-images-callback (status start end type old width ori-buffer)
  (unwind-protect 
      (let (file-data)
        (goto-char (point-min))
        (search-forward-regexp "^$")
        (setq file-data (buffer-substring-no-properties (+ (point) 1) (point-max)))
        (when file-data
          (with-current-buffer ori-buffer
            (if (and (car-safe old) refresh)
                (image-refresh (overlay-get (cdr old) 'display))
              (setq img (create-image file-data type t :width width))
              (when img
                (setq ov (make-overlay start end))
                (overlay-put ov 'display img)
                (overlay-put ov 'face 'default)
                (overlay-put ov 'org-image-overlay t)
                (overlay-put ov 'modification-hooks
                             (list 'org-display-inline-remove-overlay))
                (push ov org-inline-image-overlays))))))
    (kill-buffer)))

(defun org-display-inline-images-async (&optional include-linked refresh beg end)
  "Display inline images asynchronously.

like org-display-inline-images. But it can display http-url-images in a asynchronous way. "
  (interactive "P")
  (when (display-graphic-p)
    (unless refresh
      (org-remove-inline-images)
      (if (fboundp 'clear-image-cache) (clear-image-cache)))
    (save-excursion
      (save-restriction
        (widen)
        (setq beg (or beg (point-min)) end (or end (point-max)))
        (goto-char beg)
        (let ((re (concat "\\[\\[\\(\\(file:\\|http:\\|https:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
                          (substring (org-image-file-name-regexp) 0 -2)
                          "\\)\\]" (if include-linked "" "\\]")))
              (case-fold-search t)
              old file ov img type attrwidth width)
          (while (re-search-forward re end t)
            (setq old (get-char-property-and-overlay (match-beginning 1)
                                                     'org-image-overlay)
                  file (substring-no-properties (match-string 0) 2 -2))
            (when (image-type-available-p 'imagemagick)
              (setq attrwidth (if (or (listp org-image-actual-width)
                                      (null org-image-actual-width))
                                  (save-excursion
                                    (save-match-data
                                      (when (re-search-backward
                                             "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
                                             (save-excursion
                                               (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
                                        (string-to-number (match-string 1))))))
                    width (cond ((eq org-image-actual-width t) nil)
                                ((null org-image-actual-width) attrwidth)
                                ((numberp org-image-actual-width)
                                 org-image-actual-width)
                                ((listp org-image-actual-width)
                                 (or attrwidth (car org-image-actual-width))))
                    type (if width 'imagemagick)))
            (require 'url)
            (url-retrieve file #'org-display-inline-images-callback `(,(match-beginning 0) ,(match-end 0) ,type ,old ,width ,(current-buffer)))))))))

这样, M-x org-display-inline-images-async 就能显示http url中的图片了.

注意由于读取http url图片的速度可能会很慢,因此这里采用了异步的方式来显示.

如何使用org-agenda查看某一天做了那些事情

  1. C-c a aM-x org-agenda <RET> a 进入org-agenda界面
  2. 按d进入day view
  3. 按j,在弹出的日历中选择要查看的日期
  4. 按l,开启log-mode(若已经开启log-mode,则无需该步骤)

如何隐藏org文件中的所有代码块

完全的隐藏做不到,不过可以通过命令 M-x org-hide-block-toggle-all 将所有代码块折叠起来.

如何修改org文件中各层级的headline前显示的标志

首先,需要安装org-bullets 这个插件.

然后设置变量`org-bullets-bullet-list’

如何插入具体到时间的时间戳

当使用 C-c . 插入时间戳时,只能插入日期而没有具体到时间.

使用 C-u C-c . 则可以插入当前时间.

如何插入行合并的表格

org表格本身只能列合并,而无法行合并. 要插入行合并的表格需要使用table.el提供的表格功能(详见Tbl菜单).

org还提供了一个转换org表格到table表格的方法`org-table-create-with-table.el’ (C-c ~)

----------+-----+

----------+
----------+

----------+-----+

从任意数字开始对列表item进行编号

方法是将 [@N] 放到item的开头,这样下一个list item的编号就是N+1,依次类推 例如

2. [@2] item2
3. item3
4. item4

调用另一个org文件中的代码块

假设在si.org中有一块名为figure-1的代码块,那么可以通过,下面那样来调用

#+call: si.org:figure-1()

快速插入org date

  • 使用 C-c . fri RET 这样,插入active date
  • 使用 C-c ! fri RET 这样,插入inactive date

ispell

如何让ispell跳过某段文本不做拼写检查

默认情况下,ispell会检查整篇文本中的拼写是否正确. 但通过设置变量`ispell-skip-region-alist’的值,可以指定跳过某几段文本不做拼写检查.

`ispell-skip-region-alist’中的元素格式可以是以下几种:

  • (REGEXP)

    表示跳过匹配REGEXP的文本,不做拼写检查

  • (START-REGEXP . END-REGEXP)

    表示从匹配START-REGEXP的文本开始,一直到匹配END-REGEXP的文本结束,这这段文本,都不做拼写检查.

    其中END-REGEXP可以是string,也可以是symbol

  • (START-REGEXP END-REGEXP)

    表示从匹配START-REGEXP的文本开始,一直到匹配END-REGEXP的文本结束,这这段文本,都不做拼写检查.

    其中END-REGEXP只是string

  • (START-REGEXP FUNCTION ARGS)

    类似(START-REGEXP END-REGEXP)

    但这里使用(apply FUNCTION ARGS)返回的值作为END-REGEXP

下面是一段从Endless Parentheses中摘录下来的代码,用于设置ispell作用在org-mode中的配置

(defun endless/org-ispell ()
  "Configure `ispell-skip-region-alist' for `org-mode'."
  (make-local-variable 'ispell-skip-region-alist)
  (add-to-list 'ispell-skip-region-alist '(org-property-drawer-re))
  (add-to-list 'ispell-skip-region-alist '("~" "~"))
  (add-to-list 'ispell-skip-region-alist '("=" "="))
  (add-to-list 'ispell-skip-region-alist '("^#\\+BEGIN_SRC" . "^#\\+END_SRC")))
(add-hook 'org-mode-hook #'endless/org-ispell)

ediff

使用ediff对比同一文件中的不同文本块

通过命令`M-x ediff-region-wordwise’或`M-x ediff-region-wordwise’可以对比任何文件(包括同一个文件)中的任意两个region的内容.

dired

dired显示文件大小时使用”human readable”的格式显示

dired内部是调用`ls’命令来产生文件列表的,通过设置变量`dired-listing-switches’的值,可以更改调用`ls’命令时的参数,从而改变文件列表的显示.

默认该`dired-listing-switches’的值为”-al”,我们可以为该值添加h选项,从而达到使用”human readable”格式显示文件大小的目的.

(setq dired-listing-switches "-alh")

eshell

如何让特定的命令不保存在eshell history中

通过设置变量`eshell-input-filter’可以实现这一目的.

`eshell-input-filter’的值应该是一个判断函数,该函数接受eshell input作为参数,若该函数返回nil值,则表示该eshell input不会被记录在history中,否则记录在history中.

下面是一个从reddit中摘录的配置,用来设置所有以空格开头的eshell input都不记入history中

(setq eshell-input-filter
      (lambda (str)
        (not (or (string= "" str)
                 (string-prefix-p " " str)))))

vc

解决windows下使用vc-git提交中文注释乱码的问题.

只需要设置vc-git-commits-coding-system为’gbk即可

(when (member system-type '(ms-dos windows-nt))
  (setq vc-git-commits-coding-system 'gbk))

info

如何打开外部的info文件

使用C-u C-h i即可使用emacs info打开外部info文件

completion

如何在补全file/buffer时忽略大小写的差别

(setq read-file-name-completion-ignore-case t)
(setq read-buffer-completion-ignore-case t)

补全时,忽略某些特定扩展名的文件

可以设置`completion-ignored-extensions’来实现

desktop-save-mode

如何使用desktop-save-mode保持”scratch” buffer内容重启emacs后不变.

只需要两个步骤即可

  1. 通知desktop-save-mode保存变量`initial-scratch-message’的值
  2. 在emacs退出前,将”scratch” buffer内容赋值给`initial-scratch-message’
require ’desktop)
(add-to-list ’desktop-globals-to-save ’initial-scratch-message)
(desktop-save-mode)

(add-hook 'kill-emacs-hook
          (lambda ()
            (with-current-buffer "*scratch*"
              (setq-default initial-scratch-message
                            (buffer-string)))))

其他

在text terminal下如何调用text-based menu

M-`M-x tmm-menubar 即可

如何编辑需要root权限的文件

在编辑文件路径前加上`/sudo::`. 例如

/sudo::/etc/bashrc

当然,你的当前用户需要在sudoer组内.

emacs是如何决定文件的major mode的?

打开一个文件时,Emacs依据如下顺序来决定应该进入的Major Mode

  1. 查看buffer local的mode变量:

    第一行包含类似_*_ mode: xyz_*_,则emacs自动进入xyz-mode

  2. 查看第一行的#!标记后的解释器,根据变量interpreter-mode-alist寻找匹配mode
  3. 根据第一行的内容,在变量magic-mode-alist中寻找匹配的mode
    (add-to-list 'magic-mode-alist '("<!DOCTYPE html .+DTD XHTML .+>" . nxml-mode) )
        
  4. 根据文件名后缀,在量auto-mode-alist中寻找匹配的mode
    ;; setup files ending in “.js” to open in js2-mode
    (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
        

如何让光标无法进入minibuffer的prompt部分

minibuffer的prompt部分是不能被修改的,这样允许光标进入是没有意义的,可用加入如下配置信息达到阻止光标进入minibuffer的prompt部分.

;; don't let the cursor go into minibuffer prompt
(setq minibuffer-prompt-properties (quote (read-only t point-entered minibuffer-avoid-prompt face minibuffer-prompt)))

如何在空格和tab之间相互转换

使用tabify和untabify命令可用使文件的空格和tab相互转换,但是要注意的是,tabify和untabify在转换时 也会转换字符串中的空格和tab,这在编程时需要注意

如何让emacs自动給script加上可执行权限

Emacs若在文件的第一行找到#!声明,则认为该文件为script文件. 通过下面设置可用让Emacs自动給script文件加上可执行文件

(add-hook 'after-save-hook 'executable-make-buffer-file-executable-if-script-p)

如何关闭emacs中对按键的提示呢?

比如使用M-x helm-occur. 会提示you can run command `helm-occur’ with XXXX

执行如下代码,可以取消这种提示.

(setq suggest-key-bindings nil)

How to Fix a Stuck Emacs

若Emacs卡住了,且按C-g无效的话,可以执行以下命令强制emacs取消正在运行的东西

pkill -SIGUSR2 emacs

Show unfinished keybindings early

按键序列只按到一半时,等待一秒钟会显示已经按下的键序列并等待用户输入剩下的键序列.

通过配置`echo-keystrokes’的值,可以修改这个等待时间. This global minor mode provides a simple way to switch between layouts and the buffers you left open before you switched (unless you closed it).

It doesn’t require any setup at all more than: (0blayout-mode)

When you start Emacs with 0blayout loaded, you will have a default layout named “default”, and then you can create new layouts (<prefix> C-c), switch layouts (C-c C-l C-b), and kill the current layout (<prefix> C-k). The default <prefix> is (C-c C-l), but you can change it using: (0blayout-add-keybindings-with-prefix “<your prefix>”)

You can also customize-variable to change the name of the default session.

The project is hosted at https://github.com/etu/0blayout There you can leave bug-reports and suggestions.

Another comparable mode is eyebrowse which have been developed for longer.

如何输入重音符号

C-x 8 标点符号 字符 就能输入重音符号. 例如 C-x 8 , c 能够输入ç 使用 C-x 8 C-h 能够显示所有可以输入的重音符号

如何输入Unicode中的那些特殊字符

使用 C-x 8 RET 十六进制代码/符号名称 可以方便的输入unicode中的那些特殊字符

例如 C-x 8 RET ADDRESSED TO THE SUBJECT 输入℁

使用tab同时作为缩进和补全的触发键

默认情况下,tab只是用来缩进,但是通过以下配置

(setq tab-always-indent 'complete)

则,第一次按tab,它会缩进,但是再按一次,则表示补全