Emacs: Seven habits of effective text editing

Vim 用戶一定都看過 Seven habits of effective text editing 這篇文章。 雖然原文是針對 Vim 的,但道理都是相通的,所以我總結(jié)了這份 Emacs 版本的高效文字編輯的習(xí)慣。

本文不會過多的深入 Emacs 的配置,因?yàn)槲矣X得展示 Emacs 能做什么比怎么做更重要。

快速移動

下面是最基本的移動

key description
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 屏幕居中(以光標(biāo)為中心)
M-r 切換光標(biāo)到到屏幕的上中下位置

查找哪幾行文字里面使用到了某個特定的單詞或者短語

key description
C-r 向前搜索
C-s 向后搜索
M-s swiper search

在一個開括號和與其配對的閉括號間跳轉(zhuǎn)

key description
C-M-f 前向移動一個 s 表達(dá)式
C-M-b 向后移動一個 s 表達(dá)式

跳轉(zhuǎn)到一個變量的本地聲明的位置,尋找一個方法在哪里聲明

建立好 ctags 后(參考后文) 使用 M-. 跳轉(zhuǎn)。

減少重復(fù)輸入

你經(jīng)常要將一個詞語修改成另一個詞語

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

有一些函數(shù)或者變量很難輸入。

你可以一字不差地輸入"XpmCreatePixmapFromData"么? 使用 company mode 可以自動補(bǔ)全,不同語言可能有自己的特殊實(shí)現(xiàn)

當(dāng)你需要多次輸入一個短語或者句子的時候,有一個更快的捷徑。

Emacs 有一個錄制宏的機(jī)制。

key description
f3 or C-x ( Start recording macro
f4 or C-x ) Stop recording macro
C-x e or f4 Playback macro

訂正錯誤

或許你可以使用縮寫來訂正, 這一機(jī)制也可以被用到使用輸入縮寫來替代一個長的詞語

~/.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

文件很少獨(dú)立存在

為你的項(xiàng)目使用一個標(biāo)簽文件。

你可以快速地在項(xiàng)目中的文件間跳轉(zhuǎn),來找到函數(shù)、結(jié)構(gòu)體、類型的定義。 首先安裝 ctags (MacOS)

brew install ctags
brew link --overwrite ctags

然后執(zhí)行 projectile-regenerate-tags 之后就能使用 M-. 來跳轉(zhuǎn)

另一個強(qiáng)大的機(jī)制是使用:grep命令在一組文件中搜索你想要的內(nèi)容

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

有很多相關(guān)的命令可以用來打開、關(guān)閉窗口,切換窗口,甚至?xí)弘[藏窗口等等

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

key description
C-x 2 橫向切分窗口
C-x 3 縱向切分窗口
C-x 1 只保留當(dāng)前選中 的窗口
C-x 0 關(guān)閉當(dāng)前選中的窗口
C-x 4 C-f find-file-other-window
C-x 4 C-o display-buffer
C-x 4 . find-tag-other-window
C-x 4 0 kill-buffer-and-window
C-x 4 a add-change-log-entry-other-window
C-x 4 b switch-to-buffer-other-window
C-x 4 c clone-indirect-buffer-other-window
C-x 4 d dired-other-window
C-x 4 f find-file-other-window

顯示當(dāng)前光標(biāo)下的函數(shù)的文檔

以 Ruby 為例,安裝好 Ruby robe mode 后 C-c C-d

協(xié)同工作

這部分 Emacs 沒有對應(yīng)的內(nèi)容,因?yàn)槟憧梢栽?Emacs 中做任何事。 :)

文字是結(jié)構(gòu)化的

快速訪問文件名:行號

一般出錯信息都是 文件名:行號 的格式,可以用下面的配置來做到直接打開錯誤文件的位置。

;; 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 這樣的格式了。

養(yǎng)成習(xí)慣

卸載你之前使用的編輯器,強(qiáng)迫自己使用 Emacs, 碰到操作不方便的地方,嘗試去搜索解決方案。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容