Emacs Cheatsheet

A couple of weeks ago I have acquired the latest edition of Mickey Petersen’s “Mastering Emacs” book. For anyone looking to learn the fundamentals of “the way of Emacs”, I can highly recommend it.

While I have used Emacs for several years by now, I always felt I was stuck with pretty basic commands and not utilizing the its full potential. After having read and studied this book, I am still not an Emacs wizard, but I am definitely on the right path.

If you are looking for a guide how to configure Emacs for programming in language X or other specific environments, this book won’t help you much. In general, there are very few suggestions outside of Emacs’s included packages (which in my opinion is a good thing, as vanilla Emacs is already complex enough to really get started).

This cheatsheet does not aspire to cover every single one of Emacs' keybindings or commands, but rather this is a subset of keybindings I want to learn and use more often. As such, it does not include basic keys such as C-f, C-b etc. It also assumes familiarity with Emacs lingo. Therefore, it is nowhere near a replacement for reading “Mastering Emacs” yourself.

All the keybindings shown in this cheatsheet are Emacs defaults.

#  Prefix commands

  • Universal negative argument: C-- or M--
  • Universal numeric argument: C-N or M-N (with N being a number)

#  Movement

Key SequenceActionNotes
M-a, M-ebackward-sentence / forward-sentence
C-M-fMove forward by sexp (forward-sexp)“s-expression”
C-M-dMove down in list of sexp
C-M-uMove up in list of sexp
C-M-kKill sexp at point
C-M-nMove to next item in list of sexp
C-M-pMove to previous item in list of sexp
M-{, M-}Move to beginning / end of paragraphI’m considering to rebind these to M-[, ]
C-M-a, C-M-eMove to beginning / end of defun
C-M-vScroll up the other window
C-M-S-v, C-M-- C-M-vScroll down the other window
M-rReposition cursor to middle, top, bottom of screenCycles
C-M-lRecenters the point to middle, top, bottom of bufferCycles
M-g gGo to line
M-g cGo to character (absolute in buffer)
M-g TABGo to columnRefers to the character in current line

#  Marking

The direction of all these commands can be inverted with the universal negative argument and a universal numeric argument will mark multiple items.

Key SequenceActionNotes
C-x hMark the whole buffer
M-hMark next paragraph
C-M-hMark next defun
C-M-SpaceMark next sexp
M-@Mark next word
C-u SpaceJump to last mark
C-x C-xExchange point and mark and reactive last region

#  Editing

Remember universal numeric and negative arguments!

Key SequenceActionNotes
M-d, C-BackspaceKill word
C-kKill rest of line
M-kKill sentence
C-M-kKill sexp
C-S-BackspaceKill current lineI might rebind this to C-S-k (to be more consistent with the rest)
C-x C-ttranspose-lines
transpose-paragrahps, transpose-sentences
M-cCapitalize next wordOnly first character
M-uUppercase next wordAll characters
M-lLowercase next wordAll characters
M-=Count words, lines and characters (count-words)
C-oInsert new line (without moving point)
C-x C-oInsert all blank lines after point
M-^Join previous with current line
M-/Expand word (with DAbbrev or Hippie)
C-x TABident-rigidlyNegative and numeric arguments!
M-zzap-to-charNegative argument!
C-.Auto correct word at point (Cycles through options)
M-x ispell-buffer, ispell-regionRun spell check for buffer / region
M-x read-only-modeToggles read only mode for buffer
M-x highlight-phrase, -regexp, -symbol-at-pointHighlights a pattern

#  Bookmarks

Bookmarks also work across various special modes such as TRAMP.

Key SequenceActionNotes
C-x r mSet new bookmark
C-x r lList bookmarks
C-x r bJump to bookmark

#  Isearch

Key SequenceActionNotes
M-n, M-pMove to next / previous item in search history
C-M-iAuto-complete string from search history
M-s wToggle word modeWill ignore punctuation and delimiters while searching
M-s SpaceToggle lax whitespace matching

#  Occur

Occur mode creates a new buffer from lines in the current buffer matching a given pattern (like grep). This new buffer can then be modified and the changed lines can be written back to the original buffer.

Key SequenceActionNotes
M-s ooccur-mode
M-n, M-pGo to next / previous occurrence
<, >Go to beginning / end of occur buffer
gRefresh search results
eEnable edit mode
C-c C-cExit occur mode and apply changes to original buffer
qQuit occur mode (discards buffer)

#  EWW

Key SequenceActionNotes
M-x ewwOpen EWW
C-u M-x ewwOpen new EWW buffer
TAB, S-TABGo to next / previous hyperlink
l, rGo backward / forward in browsing history
p, n, u, tSemantic navigation (if supported by web page)
RSwitch to reader mode
&Open current page with browse-url
C-u RETOpen link at point in external browser
M-RETOpen link in new EWW buffer
wCopy link at point
M-s M-wSearch for point / region on the internet
qQuit EWW
BShow bookmarks
bAdd bookmark

#  Dired

Key SequenceActionNotes
^Goes up one directory (..)
qQuit this dired buffer
m, u, tMark active / unactive / toggle
UUnmark everything
dFlag for deletion
Region marking
* m, * uMark / unmark region
* %Mark files by regexp
* .Mark files by extension
* cChange mark
* tToggle (invert) marks
CCopy files
RRename or move files
O, G, MChange owner / group / permissions
DDeletes marked filesfiles with *
xDeletes flagged filesfiles with D
FVisit filesOpens a buffer for each marked file
cCompress marked files into archive
gRefresh buffer
+Create subdirectory
sToggles sorting by name / date
<, >Jump to previous / next directory
jJump to file
!Run shell command (synchronous)Use argument * to have all files in one command, ? for one command per file
iInsert subdirectoryShows another directory in the current dired buffer

#  Shell commands

Key SequenceActionNotes
M-!Run shell command (shell-command)
C-u M-!Run shell command and insert output into buffer
M-|Pipe region into shell command (shell-on-region)

#  Misc.

Key SequenceActionNotes
M-x lgrepGrep files specified by glob
M-x rgrepRecursively greps in specified files

#  Helping yourself

As Mickey argues in his book, probably the most important keybindings for Emacs.

Key SequenceActionNotes
M-x info-apropros, apropros-commandHelp for specific topics and commands
(prefix) C-hList all keybindings which start with “prefix”
C-h kDescribe what a key does
C-h fDescribe what a command does
C-h mDescribe current mode(s)

Happy hacking!