Emacs: Seven habits of effective text editing

Vim 用户一定都看过 Seven habits of effective text editing 这篇文章。 虽然原文是针对 Vim 的,但道理都是相通的,所以我总结了这份 Emacs 版本的高效文字编辑的习惯。

本文不会过多的深入 Emacs 的配置,因为我觉得展示 Emacs 能做什么比怎么做更重要。

快速移动

下面是最基本的移动

keydescription
C-f向前移动一个字符
C-b向后移动一个字符
C-p向上移动一行
C-n向下移动一行
C-a移动到行首
C-e移动到行尾
M-f向前移动一个单词
M-b向后移动一个单词
M-g M-g移动到特定一行
C-v下一页
M-v上一页
C-l屏幕居中(以光标为中心)
M-r切换光标到到屏幕的上中下位置

查找哪几行文字里面使用到了某个特定的单词或者短语

keydescription
C-r向前搜索
C-s向后搜索
M-sswiper search

在一个开括号和与其配对的闭括号间跳转

keydescription
C-M-f前向移动一个 s 表达式
C-M-b向后移动一个 s 表达式

跳转到一个变量的本地声明的位置,寻找一个方法在哪里声明

建立好 ctags 后(参考后文) 使用 M-. 跳转。

减少重复输入

你经常要将一个词语修改成另一个词语

如果是一个 buffer 的话,可以使用 replace-string 如果是全局的话,使用 project-replace (C-p r)

有一些函数或者变量很难输入。

你可以一字不差地输入”XpmCreatePixmapFromData”么? 使用 company mode 可以自动补全,不同语言可能有自己的特殊实现

当你需要多次输入一个短语或者句子的时候,有一个更快的捷径。

Emacs 有一个录制宏的机制。

keydescription
f3 or C-x (Start recording macro
f4 or C-x )Stop recording macro
C-x e or f4Playback macro

订正错误

或许你可以使用缩写来订正, 这一机制也可以被用到使用输入缩写来替代一个长的词语

在 ~~/.emacs.d/abbrev_defs~ 中可以定义:

(define-abbrev-table 'global-abbrev-table
  '(
    ("afaict" "as far as I can tell" nil 1)
    ("omuse" "http://www.emacswiki.org/cgi-bin/oddmuse.pl" nil 0)
    ("btw" "by the way" nil 3)
    ("wether" "whether" nil 5)
    ("ewiki" "http://www.emacswiki.org/cgi-bin/wiki.pl" nil 3)
    ("pov" "point of view" nil 1)
    ))

输入 btwC-x '~ 就会得到 ~by thw way

文件很少独立存在

为你的项目使用一个标签文件。

你可以快速地在项目中的文件间跳转,来找到函数、结构体、类型的定义。 首先安装 ctags (MacOS)

brew install ctags
brew link --overwrite ctags

然后执行 projectile-regenerate-tags 之后就能使用 M-. 来跳转

另一个强大的机制是使用:grep命令在一组文件中搜索你想要的内容

C-p-s-(g|r|s) 可以调用 projectile 的搜索功能,不限于 grep(g)。

有很多相关的命令可以用来打开、关闭窗口,切换窗口,甚至暂隐藏窗口等等

Emacs 对多窗口的支持非常好:

keydescription
C-x 2横向切分窗口
C-x 3纵向切分窗口
C-x 1只保留当前选中 的窗口
C-x 0关闭当前选中的窗口
C-x 4 C-ffind-file-other-window
C-x 4 C-odisplay-buffer
C-x 4 .find-tag-other-window
C-x 4 0kill-buffer-and-window
C-x 4 aadd-change-log-entry-other-window
C-x 4 bswitch-to-buffer-other-window
C-x 4 cclone-indirect-buffer-other-window
C-x 4 ddired-other-window
C-x 4 ffind-file-other-window

显示当前光标下的函数的文档

以 Ruby 为例,安装好 Ruby robe mode 后 C-c C-d

协同工作

这部分 Emacs 没有对应的内容,因为你可以在 Emacs 中做任何事。 :)

文字是结构化的

快速访问文件名:行号

一般出错信息都是 文件名:行号 的格式,可以用下面的配置来做到直接打开错误文件的位置。

;; Open files and goto lines like we see from g++ etc. i.e. file:line#
;; (to-do "make `find-file-line-number' work for emacsclient as well")
;; (to-do "make `find-file-line-number' check if the file exists")
(defadvice find-file (around find-file-line-number
                             (filename &optional wildcards)
                             activate)
  "Turn files like file.cpp:14 into file.cpp and going to the 14-th line."
  (save-match-data
    (let* ((matched (string-match "^\\(.*\\):\\([0-9]+\\):?$" filename))
           (line-number (and matched
                             (match-string 2 filename)
                             (string-to-number (match-string 2 filename))))
           (filename (if matched (match-string 1 filename) filename)))
      ad-do-it
      (when line-number
        ;; goto-line is for interactive use
        (goto-char (point-min))
        (forward-line (1- line-number))))))

然后就可以在 find-file 的时候使用 README.md:3 这样的格式了。

养成习惯

卸载你之前使用的编辑器,强迫自己使用 Emacs, 碰到操作不方便的地方,尝试去搜索解决方案。