用Emacs的樂趣之一就是親眼看到自己的編輯效率随使用時間的累積而增長。而且有了elisp的幫助,可以将其它編輯器的優秀功能”拷貝”過來,大有海納百川的壯闊感覺。 Babar K. Zafar的網頁上有篇 彙集Emacs技巧的文章。非常有用。我尤其喜歡裡面實作常用VI功能的函數:這樣我就不用丢掉以前用慣的編輯功能了。什麼時候Elicpse也支援自己的腳本語言就好了。讓我們輕易地讀取和操縱Eclipse Runtime以及Java AST,不用為了在console裡加上檔案連結都要建立N個類。Wiki的創造人Ward Cunningham正在做這項工作。但願他快點搞定。 下面解釋一下自己常用的幾則技巧。原文還有更多技巧,強烈推薦。 先說說怎麼試用這些技巧。Emacs根本就是elisp的解釋環境,碰巧實作了編輯器而已。是以和elisp的函數勾兌再容易不過。假設我們打開一個Emacs的緩沖:
現在c-x 2, 增加新的緩沖
接着c-x o, 把光标切換到新增的緩沖裡:
再切換到*scratch*緩沖就行了。Emacs提供的*scratch*緩沖的預設模式是elisp-mode,正适合我們做elisp的編輯和試驗。輸入指令c-x b, 再敲入scratch, 回車,就成了:
在*scratch*緩沖裡輸入代碼後,m-x eval-buffer, 輸入的代碼就被裝載運作。 下面是Babar提供的技巧:
加強搜尋 Emacs下c-s對應漸進搜尋。不過我們更多的時候需要搜尋某種模式,是以用得最多的還是漸進式的正規表達式搜尋。正規表達式搜尋有個煩人的問題:搜尋結束時光标不一定停留在比對字串的開端。幸好這個問題容易解決: (local-set-key [(control s)] 'isearch-forward-regexp)
(local-set-key [(control r)] 'isearch-backward-regexp) ;; Always end searches at the beginning of the matching expression.
(add-hook 'isearch-mode-end-hook 'custom-goto-match-beginning) (defun custom-goto-match-beginning ()
"Use with isearch hook to end search at first char of match."
(when isearch-forward (goto-char isearch-other-end))) 頭兩行重新綁定标準搜尋鍵c-s和c-r,把isearch換成regex-isearch。後面三行加入定制函數。關鍵的語句是(goto-char isearch-other-end),保證光标停留在比對字串的開頭,而不是預設的末尾。 自動完成 Emacs的hippie-expansion非常強大,可以幫我們自動完成很多重複輸入。我用慣了Eclipse和Vim,是以把自動完成CTRL-Space綁定到自動完成,而把CTRL-ENTER綁定到CTRL-space預設對應的set-mark了:
(global-set-key [(control space)] 'hippie-expand)
(global-set-key [(control return)] 'set-mark-command)
(require 'hippie-exp)
(setq hippie-expand-try-functions-list
'(try-expand-dabbrev
try-expand-dabbrev-all-buffers
try-expand-dabbrev-from-kill
try-complete-file-name-partially
try-complete-file-name
try-complete-lisp-symbol-partially
try-complete-lisp-symbol
try-expand-whole-kill))
括号自動完成 用Emacs的大部分時間都在程式設計。程式設計是少有遇到不配對的括号。既然如此,幹嘛不讓Emacs幫我們配對呢?下面的語句自動完成(, [, {, 和<。
(setq skeleton-pair t)
(local-set-key (kbd "[") 'skeleton-pair-insert-maybe)
(local-set-key (kbd "(") 'skeleton-pair-insert-maybe)
(local-set-key (kbd "{") 'skeleton-pair-insert-maybe)
(local-set-key (kbd "<") 'skeleton-pair-insert-maybe)
移動整行 這個功能是從蘋果機裡非常流行的編輯器TextMate學來的。在TextMate裡,ALT-up會把光标所在的整行文字上移一行,而ALT-down會把光标所在的整行文字下移一行。這在調整語句順序時非常有用。有了這個功能,我們可以和煩瑣的“選中全行―》剪切 -》移動滑鼠-》換行-》粘貼”告别了。 ( local-set-key [(meta up)] 'move-line-up)
( local-set-key [(meta down)] 'move-line-down)
(defun move-line (&optional n)
"Move current line N (1) lines up/down leaving point in place."
(interactive "p")
(when (null n)
(setq n 1))
(let ((col (current-column)))
(beginning-of-line)
(next-line 1)
(transpose-lines n)
(previous-line 1)
(forward-char col)))
(defun move-line-up (n)
"Moves current line N (1) lines up leaving point in place."
(interactive "p")
(move-line (if (null n) -1 (- n))))
(defun move-line-down (n)
"Moves current line N (1) lines down leaving point in place."
(interactive "p")
(move-line (if (null n) 1 n)))
換行對齊 這個看似簡單功能太有用了!!!不知道省了多少無謂的光标移動。很多時候,我們修改了一行語句後,想立刻跳到下一行,并且光标自動移到正确縮進的位置。在普通的編輯器裡,換行前光标得移動到行尾,然後我們再敲下ENTER。Vi裡CTRL-o解決了這個問題,讓我們在一行的任何地方換到下一行,自動縮進,而不用擔心把上一行分成兩半。而Emacs有了下面的配置,也就支援CTRL-o了。夠強大吧?
(local-set-key [(control o)] 'vi-open-next-line)
(defun vi-open-next-line (arg)
"Move to the next line (like vi) and then opens a line."
(interactive "p")
(end-of-line)
(open-line arg)
(next-line 1)
(indent-according-to-mode))
括号比對 也是從Vi來的酷功能。當光标在括号上是,輸入%光标自動跳到比對的另一個括号上。
(local-set-key "%" 'match-paren)
(defun match-paren (arg)
"Go to the matching parenthesis if on parenthesis otherwise insert %."
(interactive "p")
(cond ((looking-at "//s/(") (forward-list 1) (backward-char 1))
((looking-at "//s/)") (forward-char 1) (backward-list 1))
(t (self-insert-command (or arg 1)))))
拷貝目前行 很多時候我們想把光标所在的整行語句放到kill-ring(Windows下的剪貼闆)裡。但選取整行,CTRL-W還是太煩瑣。 SlickEdit裡,如果沒有任何區域被選中,COPY指令會自動拷貝光标所在的整行,CUT則剪取光标所在地整行。Emacs下可以這麼做: (defadvice kill-ring-save (before slickcopy activate compile)
"When called interactively with no active region, copy a single line instead."
(interactive
(if mark-active (list (region-beginning) (region-end))
(list (line-beginning-position)
(line-beginning-position 2)))))
(defadvice kill-region (before slickcut activate compile)
"When called interactively with no active region, kill a single line instead."
(interactive
(if mark-active (list (region-beginning) (region-end))
(list (line-beginning-position)
(line-beginning-position 2)))))