Vim 用戶一定都看過 Seven habits of effective text editing 這篇文章艺配。 雖然原文是針對 Vim 的察郁,但道理都是相通的,所以我總結了這份 Emacs 版本的高效文字編輯的習慣转唉。
本文不會過多的深入 Emacs 的配置皮钠,因為我覺得展示 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 | 屏幕居中(以光標為中心) |
M-r | 切換光標到到屏幕的上中下位置 |
查找哪幾行文字里面使用到了某個特定的單詞或者短語
key | description |
---|---|
C-r | 向前搜索 |
C-s | 向后搜索 |
M-s | swiper search |
在一個開括號和與其配對的閉括號間跳轉
key | description |
---|---|
C-M-f | 前向移動一個 s 表達式 |
C-M-b | 向后移動一個 s 表達式 |
跳轉到一個變量的本地聲明的位置赠法,尋找一個方法在哪里聲明
建立好 ctags 后(參考后文) 使用 M-.
跳轉麦轰。
減少重復輸入
你經(jīng)常要將一個詞語修改成另一個詞語
如果是一個 buffer 的話,可以使用 replace-string
如果是全局的話砖织,使用 project-replace
(C-p r)
有一些函數(shù)或者變量很難輸入款侵。
你可以一字不差地輸入"XpmCreatePixmapFromData"么? 使用 company
mode 可以自動補全侧纯,不同語言可能有自己的特殊實現(xiàn)
當你需要多次輸入一個短語或者句子的時候新锈,有一個更快的捷徑。
Emacs 有一個錄制宏的機制眶熬。
key | description |
---|---|
f3 or C-x ( | Start recording macro |
f4 or C-x ) | Stop recording macro |
C-x e or f4 | Playback 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)
))
輸入 btw
后 C-x '
就會得到 by thw way
文件很少獨立存在
為你的項目使用一個標簽文件妹笆。
你可以快速地在項目中的文件間跳轉块请,來找到函數(shù)、結構體拳缠、類型的定義墩新。 首先安裝 ctags (MacOS)
brew install ctags
brew link --overwrite ctags
然后執(zhí)行 projectile-regenerate-tags
之后就能使用 M-.
來跳轉
另一個強大的機制是使用:grep命令在一組文件中搜索你想要的內(nèi)容
C-p-s-(g|r|s) 可以調用 projectile 的搜索功能,不限于 grep(g)窟坐。
有很多相關的命令可以用來打開海渊、關閉窗口,切換窗口狸涌,甚至暫隱藏窗口等等
Emacs 對多窗口的支持非常好:
key | description |
---|---|
C-x 2 | 橫向切分窗口 |
C-x 3 | 縱向切分窗口 |
C-x 1 | 只保留當前選中 的窗口 |
C-x 0 | 關閉當前選中的窗口 |
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 |
顯示當前光標下的函數(shù)的文檔
以 Ruby 為例切省,安裝好 Ruby robe mode 后 C-c C-d
協(xié)同工作
這部分 Emacs 沒有對應的內(nèi)容,因為你可以在 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 這樣的格式了。
養(yǎng)成習慣
卸載你之前使用的編輯器驯用,強迫自己使用 Emacs, 碰到操作不方便的地方脸秽,嘗試去搜索解決方案。