Reputation: 4928
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!
Upvotes: 19
Views: 10514
Reputation: 73246
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).
Upvotes: 26
Reputation: 172894
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)))
Upvotes: 31
Reputation: 30701
Define a command that calls each of the functions (commands) that you want, conditionally. Use the prefix arg to distinguish which to call. So if the new, dispatching command is bound to, say, C-o
, then C-u C-o
would call one of the functions and C-o
(without a prefix arg) would call the other.
You will want to do C-h f interactive
, to see how to define a command that recognizes a prefix argument etc. Consult the Elisp manual also - use i interactive
to find where it teaches this.
This is an easy and fun exercise. Learning to define your own simple commands is a good way to start to talk to Emacs in its own language.
Upvotes: 1
Reputation: 319
You can define another functon with defun
in which you invoke the others with funcall
orapply
so, when you call this third function (which you can also bind) it will invoke the others.
Upvotes: 0