12 Emacs Keybindings Worth Learning

After spending a bit of time learning Emacs, I’ve found it has a widespread compatibility with a lot of the basic movement and editing commands. Spending a bit of time learning these commands can make you more effective in a variety of places, even if you don’t use Emacs as your preferred editor.

Useful Emacs Keybindings

The most basic bindings that I’ll cover here are shortcuts that let you move your cursor around and delete text. On macOS, it’s worth noting that the Option key is the Meta key. On Windows, this is usually the Alt key.

Moving and deleting by words

 

Function Keyboard shortcut Alternative in macOS
Move forward one character Control-f
Move backward one character Control-b
Delete forward from the cursor Control-d
Move forward one word Meta-f Control-Option-f
Move backward one word Meta-b Control-Option-b
Delete the last word Meta-Delete
Delete the next word (forward) Meta-d

Moving and deleting by lines

Function Keyboard shortcut Alternative in macOS
Move up one line Control-p
Move down one line Control-n
Move to the beginning of the line Control-a
Move to the end of the line Control-e
Delete to the end of the line Control-k

Where Do They Work?

It’s quite likely that a lot of the software you interact with as a developer has support for these commands hidden away. For example, all of these should work out-of-the box on your shell and most programs, especially if they use the GNU readline library.

As it turns out, many of the default Emacs commands are also implemented by the text editing system built into macOS. This makes them available pretty much anywhere you’re entering text, even in your browser’s URL bar or when you’re composing an email.

Given Emacs’ long and influential history, I’ve found that you can just try to use them somewhere, and you will often find success.

Some Example Use Cases

I often find myself using these shortcuts for scenarios such as:

  • Quickly deleting the last word I (mis)typed using M-del, before retyping it
  • Jumping back a few words on the command line so I can change or insert an argument to the program before I invoke it
  • Abandoning and deleting the line I’ve been typing with C-a C-k

Why Not VI Bindings?

I used to be one of those people who enabled VI bindings in my shell. For short editing tasks, I found the friction to be too much. It’s a lot easier to press M-b a couple times and resume typing, without the mental overhead of modality. Dealing with modality at the shell can often be quite jarring.

Beyond this, Emacs bindings are available in a lot more places.

Next Steps

Did you know there’s also a command for transposing words? It’s M-t. This one can be handy for switching arguments around at the command line. There are a lot more Emacs commands that are commonly implemented. To learn more, I suggest reading the Emacs or the GNU Readline documentation.

If you’ve ever found yourself using the arrow keys to navigate text at the command line slowly, character by character, I hope you’ll have found this post useful.