我刚接触emacs,有一个初学者问题。我可以通过(global-set-key (kbd "C-c a b c") 'some-command)
将一个按键绑定到特定的函数,其中some-command
是一个函数。如何使用一个按键绑定调用两个函数(比如说some-command
和some-other-command
)?非常感谢!
我刚接触emacs,有一个初学者问题。我可以通过(global-set-key (kbd "C-c a b c") 'some-command)
将一个按键绑定到特定的函数,其中some-command
是一个函数。如何使用一个按键绑定调用两个函数(比如说some-command
和some-other-command
)?非常感谢!
(global-set-key (kbd "C-c a b c") (lambda () (interactive) (some-command) (some-other-command)))
我建议不要将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.
如果您从未对代码进行字节编译,那么它就不太难懂,但仍然未格式化:
C-c a b c runs the command (lambda nil (interactive) (some-command)
(some-other-command)), which is an interactive Lisp function.
对于像这样的小函数,尽管仍然可读,但对于较大的函数来说很快就难以理解。
相比之下:
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.
如果你给函数命名,就会得到这个效果(这鼓励你编写比匿名函数更详细的文档)。
(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)
最后,正如 abo-abo 指出的那样,这也意味着您可以随时轻松地访问该函数的定义,以查看或编辑/重新评估代码,方法是通过在帮助缓冲区中提供的链接(在我的示例中为foo.el
),或使用M-xfind-function
(输入函数名称),或者M-xfind-function-on-key
(输入键序列它被绑定到)。
C-o
,则C-u C-o
将调用其中一个函数,而没有前缀参数的C-o
将调用另一个函数。C-h f interactive
,以查看如何定义识别前缀参数等命令。还应查阅Elisp手册-使用i interactive
查找教授此内容的位置。你可以使用defun
定义另一个函数,在其中使用funcall
或apply
调用其他函数,这样当你调用这个第三个函数(也可以绑定)时,它将调用其他函数。
'(...)
是一个列表,而不是一个函数。请参考https://dev59.com/VmQn5IYBdhLWcg3we3EE#16802304。 - Stefanfind-name-dired)
,enter
,*py
,t
,Q
.. 并从这一点继续吗?@songyuanyao - alper