I'm new to emacs, and have a rookie question. I can bind a key to a particular function by (global-set-key (kbd "C-c a b c") 'some-command)
, where some-command
is a function. How can I invoke two functions (say some-command
and some-other-command
) with one key binding? Thanks a lot!
Keys can be bound to commands either interactively or in your . emacs file. To interactively bind keys for all modes, type M-x global-set-key RET key cmd RET . To bind a key just in the current major mode, type M-x local-set-key RET key cmd RET .
M- means "meta key", (“Escape” in the lab, on other computers sometimes “Alt”). For meta commands with the Escape key, press the Escape key and then release it, then press the other key. Thus M-f stands for the keyboard sequence "press and release the Escape key", " press f".
You can use global-set-key interactively by calling it with M-x global-set-key . Type the keyboard shortcut you would like to set, then specify the name of the function you would like Emacs to call.
You can define your own function which call the two functions, and bind the key to your own function. Or use a simple lambda:
(global-set-key (kbd "C-c a b c") (lambda () (interactive) (some-command) (some-other-command)))
I recommend never binding lambda expressions to keys, for the simple reason that when you ask Emacs what that key does, it will end up telling you something like this (to use the accepted code, when byte-compiled, as an example):
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.
If you never byte-compile your code, it's less cryptic, but still unformatted:
C-c a b c runs the command (lambda nil (interactive) (some-command)
(some-other-command)), which is an interactive Lisp function.
Which, while still readable in the case of a small function like this, gets rapidly incomprehensible for larger functions.
Compared with:
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.
Which you get if you name the function (which encourages you to document it more than an anonymous function does).
(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)
Finally, as abo-abo points out, this also means you can easily visit the definition of that function at any time, to view or edit/re-evaluate the code, either by following the link provided in the help buffer (to foo.el
in my example), or by using M-x find-function
(enter the name of the function), or M-x find-function-on-key
(type the key sequence it's bound to).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With