Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs key binding for multiple commands

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!

like image 776
Ying Xiong Avatar asked Dec 03 '14 02:12

Ying Xiong


People also ask

How do you bind commands in Emacs?

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 .

What is the M key in Emacs?

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".

How do I change emacs shortcuts?

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.


2 Answers

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)))
like image 122
songyuanyao Avatar answered Oct 06 '22 01:10

songyuanyao


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).

like image 40
phils Avatar answered Oct 06 '22 00:10

phils