Emacs用于多个命令的键绑定

Yin*_*ong 13 emacs elisp key-bindings

我是emacs的新手,并且有一个菜鸟问题.我可以将一个键绑定到一个特定的函数(global-set-key (kbd "C-c a b c") 'some-command),其中some-command是一个函数.我怎样才能调用两个函数(比如some-commandsome-other-command一个键绑定)?非常感谢!

phi*_*ils 18

我建议永远不要将lambda表达式绑定到键,原因很简单,当你向Emacs询问该键的作用时,它最终会告诉你这样的事情(使用接受的代码,当字节编译时,作为例子):

C-c a b c runs the command #[nil "\300 \210\301 \207" [some-command
some-other-command] 1 nil nil], which is an interactive compiled Lisp
function.

It is bound to C-c a b c.

(anonymous)

Not documented.
Run Code Online (Sandbox Code Playgroud)

如果你从不对代码进行字节编译,那就不那么神秘,但仍然没有格式化:

C-c a b c runs the command (lambda nil (interactive) (some-command)
(some-other-command)), which is an interactive Lisp function.
Run Code Online (Sandbox Code Playgroud)

其中,而在这样的小功能的情况下仍然可读,得到迅速更大的功能难以理解.

和....相比:

C-c a b c runs the command my-run-some-commands, which is an
interactive compiled Lisp function in `foo.el'.

It is bound to C-c a b c.

(my-run-some-commands)

Run `some-command' and `some-other-command' in sequence.
Run Code Online (Sandbox Code Playgroud)

如果你命名这个函数你会得到的(它鼓励你记录它比匿名函数更多).

(defun my-run-some-commands ()
  "Run `some-command' and `some-other-command' in sequence."
  (interactive)
  (some-command)
  (some-other-command))

(global-set-key (kbd "C-c a b c") 'my-run-some-commands)
Run Code Online (Sandbox Code Playgroud)

最后,正如abo-abo所指出的,这也意味着您可以随时轻松访问该函数的定义,查看或编辑/重新评估代码,方法是按照帮助缓冲区中提供的链接(foo.el在我的帮助中)例如),或者通过使用M-x find-function(输入函数的名称),或者M-x find-function-on-key(键入它所绑定的键序列).

  • 非常重要的是,您可以使用此方法跳转到定义. (3认同)

son*_*yao 17

您可以定义自己的函数来调用这两个函数,并将键绑定到您自己的函数.或者使用简单的lambda:

(global-set-key (kbd "C-c a b c") (lambda () (interactive) (some-command) (some-other-command)))
Run Code Online (Sandbox Code Playgroud)

  • @songyuanyao:因为''(...)`是*list*,而不是*function*.请参阅http://stackoverflow.com/questions/16801396/when-should-emacs-function-syntax-be-used/16802304#16802304 (2认同)