emacs cheatsheet

2020-10-02

 | 

~5 min read

 | 

824 words

A course I’m taking strongly encourages the use of emacs, so I now have an opportunity to learn that along with vim! I suspect I will never become a super user in either (though I’m more inclined to give it a go with vim at the moment), but that’s okay. My goal is to be competent enough to be able to work in any environment. To that end, I’ll keep track of some of the lessons I’m learning so that I have a reference.

Understanding The Editor

emacs editor

Common Command Shortcuts

Emacs uses (Control) and Meta1. Often times, these are written simply as C and M respectively.

This means that C-x and ⌃-x are the same. For simplicity, I will use the C and M convention.

The most important commands in Emacs:

  • C-x C-c: Quit Emacs
  • C-g: Cancel the current action
  • C-x C-f: Visiting (Open) a file (whether or not it already exists) - More on Visiting files
  • C-x C-s: Save a file
  • C-x C-w: Write a file (probably more familiar to you as Save as…)

Cut, copy, paste, undo, and editing:

  • C-Space: Set a mark (this is useful for highlighting. By setting a mark you are then able to navigate and highlight the text under the selection.)
  • C-w: Cut a highlighted region
  • M-w: Copy a highlighted region
  • C-k: Cut (kill) from the cursor to the end of the line
  • C-y: Paste (yank)
  • C-x u or C-_: Undo
  • <DEL>: Delete the character just before the cursor
  • C-d: Delete the next character after the cursor
  • M-<DEL>: Kill the word immediately before the cursor
  • M-d: Kill the next word after the cursor
  • C-k: Kill from the cursor position to end of line
  • M-k: Kill to the end of the current sentence

File Navigation

  • C-p: Previous line
  • C-b: Backward
  • M-b: Backward one word
  • C-f: Forward one character
  • M-f: Forward one word
  • C-n: Next line
  • C-v: Move forward one screen
  • M-v: Move backward one screen
  • C-l: Clear the screen (i.e. recenter the cursor in the center of the screen)
  • M-<: Jump to the beginning of the file (note, that’s actually <, which means ⇧+,)
  • M->: Jump to the end of the file (note, that’s actually >, which means ⇧+.)

Buffer Navigation and Management

  • C-x b: Switch to another buffer by entering its name
  • C-x C-b: See a list of all current buffers
  • C-x <left>: Navigate to the previous buffer
  • C-x <right>: Navigate to the next buffer
  • C-x k [buffer] ret; Kill [buffer]
  • M-x kill-some-buffers - Offer to kill buffers one-by-one
  • M-x kill-matching-buffers - Kill buffers matching a regular expression
  • M-x clean-buffer-list - useful for purging unmodified / stale buffers

More on killing buffers.

Window Navigation

  • C-x 2: Create a new window below
  • C-x 3: Create a new window on right
  • C-x 0: Close current window
  • C-x 1: Close other windows
  • C-x o: Goes to the other-window. There are a number of solutions, but they all require plugins (some built-in) and configuration. For more of a discussion, see this StackExchange Q&A.

Some other useful commands:

Getting help within Emacs:

In addition to the help button/menu on the right…

  • C-h: Hitting this will display a short message in the minibuffer: 𝙲-𝚑 (𝚃𝚢𝚙𝚎 ? 𝚏𝚘𝚛 𝚏𝚞𝚛𝚝𝚑𝚎𝚛 𝚘𝚙𝚝𝚒𝚘𝚗𝚜).
  • C-h b: Key bindings. This lists all key bindings that are valid for the current mode. Note that key bindings change from mode to mode.
  • C-h a: Command apropos. After typing C-h a you can type a symbol and a buffer will appear that lists all symbols and functions that match that phrase.

Footnotes

  • 1 Mac keyboards do not have a Meta key be default (it’s typically an Alt key), so the default is the ESC (which is what Meta is converted to by Emacs anyway). If you do not want to use the ESC key as the Meta, however, you can rebind it with the setting, “The Command key acts as the Emacs Meta key (source: Specifying A Meta Key). An easier way is to rebind it within your terminal. I use ITerm2, where the steps are described in the FAQ:

    Q: How do I make the option/alt key act like Meta or send escape codes?

    A: Go to Preferences > Profiles tab. Select your profile on the left, and then open the Keyboard tab. At the bottom is a set of buttons that lets you select the behavior of the Option key. For most users, Esc+ will be the best choice.

    A similar solution is available for Mac’s native Terminal application as described in this Stack Overflow answer.


Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!