Emacs keystrokes

From LQWiki
Jump to: navigation, search

Emacs works its text-editing wonders through a combination of indirection and Lisp-jitsu. When you type a key--'k', for instance--Emacs doesn't simply paint the character on the screen, it takes the keystroke and sees what sort of commands are "bound" to it. In the case of 'k', the command it issues is a straightforward "insert me where the cursor is."

At this point, no advantage is apparent. But the commands bound to a keystroke can be arbitrarily complex. For example, when Emacs is running in C mode, pressing the <TAB> key doesn't indent everything after the cursor by a single tabspace. Instead, it analyzes the code that you are writing, and indents the entire line to whatever depth appears to make the most sense.

There are many more examples, but the upshot is that if you're willing to learn the Emacs way, there are many tips and tricks to pick up that can make it a very productive environment. Just be warned that, before it's over, you're going to be bending and contorting your fingers in ways you never imagined.

Basic Keystrokes

The keystroke <CTRL>+X is generally represented as C-x, while <META>-v would be displayed with the shorthand M-v. The meta key varies from system to system, but is usually the <ALT> key. The <ESC> key can be pressed before any key to simulate the <META> version of that key.

If two commands are separated by a comma (C-p, C-n for example) then they are separate commands. Otherwise they are part of the same command. For example, C-x C-s means you type <CTRL>-x, then <CTRL>-s to execute the command.


  • C-p, C-n, C-f, C-b : Functionally equivalent to the arrow buttons. C-p moves the cursor up to the previous line, C-n moves it down to the next line, C-f moves one space forward, and C-b moves one space backwards. It may seem silly to have these commands when the arrow keys are right there. But once you're used to them [and they do take some getting used to] you may decide that 'right there' just isn't close enough, and it's nice to be able to navigate without moving your hands away from normal typing position.
  • C-a, C-e : C-a moves to the beginning of a line (a is the beginning of the alphabet), while C-e moves to the end of the line.

Note: These keystrokes aren't specific to Emacs. For example, all six work on the command line, and they also work inside forms on the web browser I'm using to type this. You may find it advantageous to try and make them second nature.

  • C-l : Centers your view of the buffer so that the cursor is in the middle of the screen.
  • C-v, M-v : Move the cursor up and down by half a page of text at a time.
  • M-<, M-> : Moves the cursor to the beginning and end of the entire document.

Files and Buffers

  • C-x C-s : Saves the current buffer. If there is no file associated with the buffer, Emacs prompts for a filename.
  • C-x C-w : Saves the current buffer, requesting a name.
  • C-x C-f : Open a file.
  • C-x d : Open a directory.
  • C-x b, C-x C-b: The first takes you to another buffer. the second takes you to a list of all available buffers.
  • C-x 1, C-x 2, C-x 3 : C-x 2 and 3 split the current window. 2 splits horizontally, while 3 splits vertically. C-x 1 gets rid of the various splits, leaving you with a single window.
  • C-g : Cancel the current command or key sequence, even if you are in a minibuffer. A minibuffer is a one-line buffer at the bottom of the screen, where you type in things like filenames and search expressions. Sometimes you may have to hit C-g more than once.

Cutting and Pasting

  • C-k : Removes the portion of the line following the cursor and places it on the clipboard. Repeat to remove more text and append it to the clipboard.
  • C-[space] : Sets a "mark" (beginning of the current selection).
  • C-w : Removes everything between the cursor and the "mark" and places it on the clipboard.
  • C-y : Inserts the contents of the clipboard into the buffer.
  • M-y : After pressing C-y, use this to gain access to previous items in the clipboard.


  • C-h t : This takes you to a tutorial which tries to teach you all this, and a bunch of other stuff besides. It's very helpful for the aspiring Emacs enthusiast.
  • C-h k : Runs the command "describe-key." It prompts you for a key that you would like described, then you perform whatever key sequence you want to know about.
  • C-h b : Opens a list of all the commands currently bound to keys.
  • M-x doctor : Sometimes you just have to talk out your problems with a sympathetic program.


  • M-/ : Autocompletion. Emacs looks through the buffer for possible completions of whatever you're typing at the moment. For example, if you've typed "rotweiler" at some point previous, and wish to reuse the word, you could type rotM-/ and it will fill in the "weiler". Of course, it may also come up with "rotation" if it sees that word as well. Can be very handy.

see also