在我的.emacs中,我有以下功能来转置一条线
(defun move-line (n) "Move the current line up or down by N lines." (interactive "p") (let ((col (current-column)) start end) (beginning-of-line) (setq start (point)) (end-of-line) (forward-char) (setq end (point)) (let ((line-text (delete-and-extract-region start end))) (forward-line n) (insert line-text) ;; restore point to original column in moved line (forward-line -1) (forward-char col))))
我像这样绑定一把钥匙
(global-set-key (kbd "M-") 'move-line) ;; this is the same as M-x global-set-key
但是,我想将M-up绑定到移动线(-1)但是我似乎无法正确地做到:
;; M-- M-1 M-x global-set-key
如何使用global-set-key调用move-line -1来定义上述内容?
在提出问题之后几分钟我就通过复制+粘贴代码来解决问题.但是我不知道它是如何工作的.
(global-set-key (kbd "M-") (lambda () (interactive) (move-line -1)))
global-set-key只接受2个参数:键序列和要绑定到它的命令.所以
(global-set-key (kbd "M-") 'move-line)
工作良好.但是如果你想使用带参数的move-line,你需要将它包装在一个匿名(aka lamba)函数中,以便它将global-set-key作为一个值呈现给自己.