Emacs keybindings

Author

Marie-Hélène Burle

In this section, we will explore the endlessly humoristic topic of Emacs keybindings.

On Emacs keybindings

One of the strengths and weaknesses of Emacs are its keybindings (kbd). Strength because everything can be bound to a kbd and kbd are—as everything else in Emacs—fully customizable. This means that you can make Emacs truly your own and work on text very quickly from the keyboard. Weakness because the default kbds are overwhelming to new users and the gymnastics they involve has lead to a lot of jokes.

Kbd notations

First of all, a note about notations:

  • C-c means press the Control key and the C key together,
  • M-x means press the Alt (Windows) or Option (MacOS) key and the X key together,
  • C-c m means press the Control key and the C key together, then press the M key,
  • C-c C-x m means press Ctl+C, then Ctl+X, then M,
  • DEL means press the Backspace key,
  • SPC means press the Space bar,
  • S-SPC means press Shift and the Space bar together,
  • ESC means press the Escape key,
  • s-t means press the Window key (Windows) or Command key (MacOS) and the T key together,
  • C-x C-c M-w C-m M-v M-t M-u means that you probably should choose another kbd.

Main kbds

For the rest of this course, you should have this page open in a separate tab (or even monitor if you can) as the reference you will need to look at constantly as we work in Emacs. Within a few days though, you will know these and they will become second nature—so much so that you will start truly missing them when you work outside Emacs!

Most important

C-g         Cancel beginning of command
C-x C-c     Save file and exit Emacs
C-z         Send Emacs to the background to get back to the terminal
            The Emacs window can be brought back from the terminal with: fg (foreground)
ESC ESC ESC Get out command (exit minibuffer, close other windows, etc.)

Buffers

C-x b       Switch buffer
C-x C-b     List buffers
C-x k       Kill buffer

Windows

C-x 1       Delete other windows
C-x 2       Split window below
C-x 3       Split window right
C-x o       Jump to other window
C-M-v       Scroll other window

Files

C-x C-f     Find file
C-x C-s     Save file

Navigation

C-f         Move forward one character
C-b         Move backward one character

C-p         Move to previous line
C-n         Move to next line

C-a         Move to beginning of line
C-e         Move to end of line

M-a         Move to beginning of sentence
M-e         Move to end of sentence

M-f         Move forward one word
M-b         Move backward one word

C-v         Move forward one screenful
M-v         Move backward one screenful

C-l         Center text around cursor

M-<         Move to beginning of buffer
M->         Move to end of buffer

C-u         Universal argument:
C-u 3 C-f   Move forward three characters
C-u -4 C-f  Move backward four characters
C-u 5 M-e   Move forward five sentences
C-u 6 t     Type six characters t

Editing

DEL         Delete character before cursor
C-d         Delete character after cursor

M-DEL       Kill word before cursor
M-d         Kill word after cursor

C-k         Kill to end of line
M-k         Kill to end of sentence

C-SPC       Set mark to select region
C-x h       Select all

C-w         Kill region (cut)
M-w         Copy region
C-y         Yank killed text (paste)
M-y         Following C-y: go back in kill ring to yank

C-/         Undo (to redo, use C-g followed by another C-/)

Searching

C-s         Increamental search forward (repeat for next occurance)
C-r         Incremental search backward (repeat for previous occurance)
M-n         While in search: go forward in search history
M-p         While in search: go backward in search history

Emacs was so influential in the early days of computing that many other software actually use Emacs kbds. This is the case for instance of all shells, REPLs, terminals, and consoles.

Next time you are in Bash, or in your Python/Julia/R shell, try the commands above and you will see that many of them will work.

All of these kbds are of course customizable.

This list is not exhaustive. You can also associate a new kbd to any command.

Emacs pinky

Because so many Emacs kbds involve the Control key, it can be very tiresome for the pinky finger. Most Emacs users remap their keyboard to have the Caps lock key into another Control key. This page gives information on how to do this with most operating systems.

Practice

Let’s practice those common kbds thanks to the Emacs tutorial: launch Emacs, navigate to the link “Emacs tutorial” (use C-n four times for that), and press Enter. This will open a buffer with the Emacs tutorial. The tutorial covers the kbds above and provides an opportunity to play with an Emacs text.

Executing commands without kbds

Another way to execute commands interactively is to type M-x (this brings up the minibuffer, a place in which to type inputs) followed by the command name. This is useful if a command is not bound to a kbd or if you don’t remember its kbd.

For example, M-x count-words will output the number of lines, sentences, words, and characters of the current buffer in the echo area.

Commands are series of words separated by hyphens (-), but you can type spaces instead: Emacs will add the hyphens automatically for you.

After you have entered M-x, you can use the tab key for autocompletion and you can use the kbds M-p and M-n to go back and forth in the command history.