emacs入門技巧:常用定製函數

來源:互聯網
上載者:User
 

用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對應漸進搜尋。不過我們更多的時候需要搜尋某種模式,所以用得最多的還是漸進式的Regex搜尋。Regex搜尋有個煩人的問題:搜尋結束時游標不一定停留在匹配字串的開端。幸好這個問題容易解決:(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)))))

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.