Learning Emacs Key Bindings

This post describes my method to learn Emacs native keybindings from an ex-Vimmer’s perspective. It’s an extension from a Reddit post.


Evil-mode journey

My Emacs journey started with Spacemacs. At that time, I had the misconception that Emacs has lengthy and incomprehensible keybindings, so without a second thought, I chose to go with Vim-bindings, which I used for 6 years and even read a book about. Spacemacs is surely a great project, opening up all the goodies of Emacs to a Vimmer. The iconic mnemonics system is effective, at least at the very beginning. However, over time I found that:

  1. Mnemonics requires more key presses: SPC f f (3) vs C-x C-f (2 keys, or 2.5?), SPC h d f (4) vs C-h f (2). Most search tools like ag, rgrep requires 4 key presses. Most of the time we also need an extra ESC to enter normal state. Mistype rate is higher as a result, and gets frustrating quickly.

  2. Commands from various packages are placed under same key prefix SPC letter, making it difficult to form an overall picture of what things each package does/can do, which is essential for customization in the long term.

  3. The consideration of how commands should be organized evolves over time, so user needs to adapt accordingly. I personally recommend my Spacemacs friends to watch Spacemacs ABC Series, it’s gorgeous, but recorded 3 years ago, many keybindings have changed since.

These problems gave me a headache, urging me to write my own config. I could have tried evil-mode, but having seen too many evil-mode-related compatibility issues in Spacemacs project, I did not think my poor Elisp skills can support me through the extra mess added on top of writing a config from scratch.

So the dilemma in front of me is: continue with Spacemacs mnemonics, or go with Emacs native bindings? I feel lucky now that I chose to give Emacs binding a try.

Demystifying Emacs bindings

There are countless jokes and comics on Emacs’s seemingly ridiculous keybindings. Good laughs indeed, but at the end of day, it’s not incomprehensible. It has well-defined conventions listed at:

Emacs Key Bindings Convention

In summary, the general rules are:

  1. C-x reserved for Emacs native essential keybindings: buffer, window, frame, file, directory, etc…
  2. C-c reserved for user and major mode:
    • C-c letter reserved for user. <F5>-<F9> reserved for user.
    • C-c C-letter reserved for major mode.
  3. Don’t rebind C-g, C-h and ESC.

I find this convention extremely useful, as it highlights where to look at when reading Emacs bindings. For example, in org-mode, the command org-insert-link is bind to C-c C-l. At first sight it seemed complex: I need to remember to press Control (“not Alt”), and keep it pressed, and pressed l again… but the idea is actually short:

  1. I’m trying to call a major-mode specific command (org-*), so I should press C-c first.

  2. Major-mode commands should use C-letter.

  3. (Same-old) mnemonics: l ink!

Then the only thing I need to remember is actually just: l. A much more succinct mindmap, isn’t it? Longer bindings are no more daunting. For example, in ESS (mode for statistical languages such as R, Stata), ess-load-library is bound to C-c C-e C-l. Seems long, but all it says is just 2 keys with mnemonic: l(oad) under e(xtra) in major-mode! All C-c C-{letter}-like bindings became just a letter away.

With these thoughts in mind, I find it much easier to understand bindings such as C-x C-f, C-x C-s, C-c C-n (normally assigned to “(n)ext block/headings/title” in major-mode), C-c C-k (normally assigned to (k)ill-related commands in major-mode), C-c M-w (copy a syntactic unit in major-mode), etc. You will certainly notice more of these patterns.

Of course there are exceptions: in the above example, the companion command for org-insert-link is org-store-link, which is suggested to bind to C-c l, occupying one of my customized binding space. But now at least 1) I know why it’s not bound by default, and why it suggests this binding; 2) I know I have the freedom to bind to other keys. That’s where one’s customization starts off.

Dipping the water (or making the jump)

The debate over keybinding style is a never-ending one. Coming from a Vim background, I do miss the compositionality it provides. In terms of editing only, Vim is really hard to beat.

Turning to Emacs, though, reveals to me a different philosophy where commands are first-class citizens. It seems to me that, instead of only asking “how do I manipulate these text characters”, Emacs is focusing more on “what commands I should use in this context to achieve my goal”. It emphasizes on being familiar to what a package does exactly, which commands it provdes, and what each command do, then combine them in a memorable way. Keybindings are merely representation to flows of thoughts.

I find this philosophy resembles much to UNIX’s “do one thing and do it well”. With that thought, I started to study each package in depth, and assign them bindings that fit in my workflow. For example, these are the packages I assign to utilize C-c letter space: p for projectile, g for ma(g)it, m for mu4e, r for visual-(r)egexp, w for eyebrowse (used as workspace), etc. With the keybinding convention, commands tailored for different purposes are arranged in a consistent, meaningful and mnemonic way, reducing mental efforts greatly. My editing flow then becomes chaining commands with intentions: “I wish to open the file (C-x C-f), regenerate the project tags (C-c p R), and take a look at magit status (C-c g s)“.

To wrap up, I like to perceive the process of learning keybindings as acquainting oneself with the layout of a toolbox: Calling a command is like knowing where your tool lies in the box, and a clear layout improves productivity vastly. Native Emacs bindings is the layout designed by the toolbox creators. It’s actually comprehensible and reasonable. It’s mnemonic at many aspects. Its C-c bindings for major-mode is just the same idea of leader key. I haven’t mentioned the normal navigation keys, but if you are someone working with terminals all day, its consistency with terminal bindings is also a huge plus.

Hope these are useful for new Emacs users, or Vimmers/Spacemacsers who had never tried Emacs native key bindings. Now give it a go!

comments powered by Disqus