Imagine that you hold Control and type your name into Emacs. Can you describe what will happen?
– The ‘Emacs Guru Test’
Emacs shortcuts (known as ‘key bindings’) can seem ridiculous to beginners. Some Emacs users even argue you should change them as soon as you start using Emacs.
They are wrong. In this post, I’ll describe the logic behind the Emacs key bindings. Not only will you be closer to passing the guru test, but you might even find you like some of the defaults!
There Are How Many?
Emacs has a ton of key bindings.
Emacs is a modal editor, so most key bindings are mode-specific. However, my current Emacs instance has well over a hundred global shortcuts that work everywhere.
(Keymaps are nested data structures, so this actually undercounts! For
C-h C-h and
C-h f are not counted separately.)
Even that is a drop in the bucket compared with how many commands we could define key bindings for.
How can we possibly organise all these commands?
Mnemonic Key Bindings
Basic commands are often given key bindings based on their name. You’ll encounter all of these important commands in the Emacs tutorial.
Mnemonics are a really effective way of memorising things. If you can remember the name of the command, you can probably remember the key binding too.
Organised Key Bindings
Many Emacs movement commands are laid out in a consistent pattern.
For example, movement by certain amount:
Moving to the end of something:
Transposing, which swaps text either side of the cursor:
Have you spotted the pattern?
The pattern here is that
C-whatever commands are usually
small, dumb text operations.
M-whatever commands are larger, and
usually operate on words.
C-M-whatever commands are slightly magical. These commands
understand the code they’re looking at, and operate on whole
expressions. Emacs uses the term ‘sexp’ (s-expression), but these
commands usually work in any programming language!
Discovering Key Bindings
What happens when you press
C-a? Emacs can tell you.
C-h k C-a
will show you exactly what command is run.
If you use a command without its key binding, Emacs will helpfully remind you there’s a shortcut available.
You can even do this backwards! If Emacs has done something neat or
unexpected, you might wonder what command ran.
C-h l will reveal
what the command was, and exactly which keys triggered it.
Room For Emacs
Why are Emacs key bindings different from conventional shortcuts? Why
C-c copy text to the clipboard, like many other programs?
Emacs uses mnemonics for its clipboard commands: you ‘kill’ and ‘yank’
text, so the key bindings are are
C-y. If you really want,
you can use
C-x acts as you expect.
The problem is that Emacs commands are too versatile, too general to
fit in the usual
C-v. Emacs has four clipboard
kill: remove text and insert it into the kill-ring. This is like clipboard cut, but you can do it multiple times and Emacs will remember every item in your clipboard.
kill-ring-save: copy the selected text into the kill-ring. This is like clipboard copy, but you can also do this multiple times.
yank: insert text from the kill-ring. This is like clipboard paste.
yank-pop: replace the previously yanked text with the next item in the kill ring. There is no equivalent in a single-item clipboard!
The generality of Emacs means that it’s hard to find a key binding for
everything. Key bindings tend to be slightly longer as a result:
opening a file is
C-x C-f, an additional keystroke over the
Room For You
With all these key bindings already defined, what bindings should you use for your personal favourite commands?
Much like IP addresses
192.168.x.x is reserved for private use,
keys that are reserved for
user configuration. All the sequences
C-c LETTER, such as
are reserved for your usage, as are
<F5> through to
For example, if you find yourself using
imenu a lot, you might bind
You Make The Rules
Emacs will even try to accommodate this. If you open the tutorial after changing a basic key binding, it will update accordingly!
The secret to mastering Emacs is to remember everything is
self-documenting. Learn the help commands to find out which commands
have default key bindings. Consider following the existing patterns
when you define new key bindings or override existing
org-mode, for example, redefines
C-M-t to transpose org
Once you understand the patterns, you’ll know when to follow and when to break them. You’ll also be much closer to passing that guru test!