Skip to content

Instantly share code, notes, and snippets.

@dherman
Created August 2, 2012 16:22
Show Gist options
  • Star 75 You must be signed in to star a gist
  • Fork 25 You must be signed in to fork a gist
  • Save dherman/3238368 to your computer and use it in GitHub Desktop.
Save dherman/3238368 to your computer and use it in GitHub Desktop.
My emacs cheat sheet

In penance for cracking stupid jokes on Twitter, here's my Emacs cheat sheet. Emacs has a steep learning curve, so I've tried to order them by importance so you could learn them in stages.

One overall rule of thumb: pay attention to the minibuffer (the line at the bottom of the editor). It will often guide you through a process, and also gives you hints about what state you're in, such as the middle of a multi-chord sequence.

The other rule of thumb: when in doubt, C-g it out.

Basics (mandatory)

You simply can't get by without having these at your fingertips.

  • C-x C-c - quit
  • C-x C-s - save buffer
  • C-x C-f - open file
  • C-x b buffer name - switch to open buffer
  • C-g - cancel
  • C-x k - close current buffer
  • C-h a command name - look up docs for command
  • M-x command name - execute command
  • C-x u - undo
  • C-/ - undo

Getting around (really useful, worth learning)

Navigation

  • C-a - beginning of line

  • C-e - end of line

  • C-f - forward character

  • C-b - backward character

  • C-p - down a line

  • C-n - up a line

  • M-f - forward word

  • M-b - backward word

  • S-any of the above - navigate and select

  • C-space - start selection

Cutting and pasting

  • C-w - cut
  • C-y - paste

Search and replace

  • C-s - search
  • M-% - search and replace

More advanced editing

Deletion and cutting

  • C-d - delete character ahead
  • M-d - delete word ahead
  • backspace - delete character behind
  • M-backspace - delete word behind
  • C-k - cut from cursor to end of line

Insertion

  • C-o - insert newline after cursor

Frames

  • C-x 2 - split window horizontally
  • C-x 3 - split window vertically
  • C-x 1 - unsplit window
  • C-x o - switch to other pane in split window

Tricks

  • M-q - auto-hard-wrap current paragraph
  • C-t - swap the two characters at the cursor
  • M-u - uppercase the word at the cursor
  • M-l - lowercase the word at the cursor
  • C-u n char - insert n copies of char

Power tools

  • M-x replace-regexp - search and replace by regexp (the quoting/escaping is so weird that this always takes me several tries)
  • C-r t - "string-rectangle" (this one's really weird but super useful sometimes; look up the docs)
@mbriggs
Copy link

mbriggs commented Aug 20, 2012

the joke is funny because its true, but also because its totally mystifying to anyone new to emacs :)

@chrisbanm
Copy link

That should be C-x r t not C-r t which will perform a reverse search.

@BrainStormCenter
Copy link

For those of us new to emacs, which keys do the 'C' and 'M' represent?

@mbroadhead
Copy link

mbroadhead commented Feb 13, 2018

@BrainStormCenter

For those of us new to emacs, which keys do the 'C' and 'M' represent?

C=Control and M=Alt on my keyboard. But see here for more details:

Although only the <Control> and <META> modifier keys are commonly used, Emacs supports three other modifier keys. 
These are called <Super>, <Hyper> and <Alt>. Few terminals provide ways to use these modifiers; the key labeled <Alt> 
on most keyboards usually issues the <META> modifier, not <Alt>.

@jakewilliami
Copy link

I'm not that new to emacs, but I still don't get the joke from Twitter. Can you explain? Also, nice cheat sheet!

Copy link

ghost commented Sep 23, 2022

I don't get the joke

@jakewilliami
Copy link

@blessing4538 I think it’s because Emacs Lisp (the language in which you configure Emacs) is a language that, just like other Lisp-based languages, necessarily uses open and closing parentheses in it’s syntax. Hence, getting started is as simple as (.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment