不知道你有沒有碰到過這樣的需要,就是在用兩個視窗(在emacs或者vi中的window)編寫兩個檔案時 ,忽然覺得應該把兩個表單的內容(buffer)互換一下,方便查看,這在gvim中,使用Ctrl-w r就可以完成。
---號稱無敵的emacs沒有這個內建的功能~~
還有一個功能在textmate(macOS和ruby扇子的最愛) 和eclipse的編輯器中有有,就是選定一些文本行,再按alt + up或者alt+down,被選中的文本就會在buffer中逐行穿梭。這在修改程式時相當實用。
---誒,號稱無敵的emacs又沒有這個內建的功能~~
不過emacs之所以號稱無敵就是因為我們可以很輕鬆實現這兩個功能——在emacs lisp中。
(defun circle-windows ()
"旋轉當前frame中的所有windows"
(interactive)
(let ((owindow (selected-window))
(obuffer (current-buffer))
)
(while (not (equal owindow (next-window)))
(set-window-buffer (selected-window) (window-buffer (next-window)))
(select-window (next-window)))
(set-window-buffer (selected-window) obuffer)
(select-window owindow)))
(defun move-region-around (direction beg end)
(let (real-beg
real-end
target-beg
deactivate-mark
text)
(save-excursion
(goto-char beg)
(setq real-beg (line-beginning-position))
(when (equal direction 'up)
(setq target-beg (line-beginning-position 0)))
(goto-char end)
(setq real-end (line-beginning-position 2))
(when (equal direction 'down)
(setq target-beg (copy-marker (line-beginning-position 3)))) ;must use marker
(setq text (buffer-substring-no-properties real-beg real-end))
(delete-region real-beg real-end)
(goto-char target-beg)
(insert text)
)
(set-mark (+ target-beg (- real-end real-beg 1)))
(goto-char target-beg)
(setq transient-mark-mode 'only)))
(defun move-region-up (beg end)
(interactive "r")
(move-region-around 'up beg end))
(defun move-region-down (beg end)
(interactive "r")
(move-region-around 'down beg end))
(global-set-key (quote [M-up]) (quote move-region-up))
(global-set-key (quote [M-down]) (quote move-region-down))
(global-set-key "/C-c2" 'circle-windows)
註:alt+up/down的使用和textmate是一樣的,在區區的辦公室PC上,還把alt+left/right也實現了,同事說過於華麗不實用,所以就不貼;
表單互換的熱鍵是ctrl-c 2,這並不是一個很好的設定,不過ctrl-w是區區用慣的backward-delete-word不能改~