Programming
Emacs

Getting started {.cols-3}

Starting Emacs

To enter Emacs, just type its name:

$ emacs

--
C-zSuspend Emacs
C-x C-cExit Emacs permanently
{.shortcuts}

Global Description {.secondary}

--
C-<key>Means hold the control, and press <key>
M-<key>Means press the Esc once, and press <key>
{.shortcuts}

NOTICE: This cheatsheet follows the above rules.

Motion {.row-span-2}

BackwardForwardEntity to move over
C-bC-fHaracter
M-bM-fWord
C-pC-nLine
C-aC-eLine beginning
(or end)
M-aM-eSentence
M-{M-}Paragraph
C-x [C-x ]Page
C-M-bC-M-fSexp
C-M-aC-M-eFunction
M-<M->Buffer beginning
(or end)
{.shortcuts .show-header}

Case Change

--
M-uUppercase word
M-lLowercase word
M-cCapitalize word
C-x C-uUppercase region
C-x C-lLowercase region
{.shortcuts}

Files

--
C-x C-fRead a file into Emacs
C-x C-sSave a file back to disk
C-x sSave all files
C-x iInsert contents of another file into this buffer
C-x C-vReplace this file with your file
C-x C-wWrite buffer to a specified file
C-x C-qToggle read-only status of buffer
{.shortcuts}

Error Recovery

--
C-gAbort partially typed or executing command
M-x recover-sessionRecover files lost by a system crash
C-x u
C-_
C-/
Undo an unwanted change
M-x revert-bufferRestore a buffer to its original contents
C-lRedraw garbaged screen
{.shortcuts}

Transposing {.row-span-2}

--
C-tTranspose characters
M-tTranspose words
C-x C-tTranspose lines
C-M-tTranspose sexps
{.shortcuts}

Scroll

--
C-vScroll to next screen
M-vScroll to previous screen
C-x <Scroll left
C-x >Scroll right
C-lScroll current line to
center, top, bottom
{.shortcuts}

Goto

--
M-g gGoto line
M-g cGoto char
M-mBack to indentation
{.shortcuts}

Marking

--
C-@
C-SPC
Uet mark here
C-x C-xExchange point and mark
M-@Set mark arg words away
M-hMark paragraph
C-x C-pMark page
C-M-@Mark sexp
C-M-hMark function
C-x hMark entire buffer
{.shortcuts}

Killing and Deleting {.row-span-2}

BackwardForwardEntity to kill
DELC-dCharacter
(delete)
M-DELM-dWord
M-0 C-kC-kLine
(to end of)
C-x DELM-kSentence
M-- C-M-kC-M-kSexp
{.shortcuts .show-header}

Killing

--
C-WKill region C-w
M-wCopy region to kill ring
M-z charKill through next occurrence of char
C-yYank back last thing killed
M-yReplace last yank with previous kill
{.shortcuts}

Getting Help

--
C-x 1Remove help window
C-M-vScroll help window
C-h aApropos: show commands matching a string
C-h kDescribe the function a key runs
C-h fDescribe a function
C-h mGet mode-specific information
{.shortcuts}

The help system is simple. Type C-h (or F1) and follow the directions. If you are a first-time user, type C-h t for a tutorial.

Multiple Windows {.col-span-2}

When two commands are shown, the second is a similar command for a frame instead of a window.

---
C-x 5 1C-x 1Delete all other windows
C-x 5 2C-x 2Split window, above and below
C-x 5 0C-x 0Delete this window

| C-x 3 || Split window, side by side | | C-M-v || Scroll other window |

| C-x 5 o | C-x o | Switch cursor to another window | | C-x 5 b | C-x 4 b | Select buffer in other window | | C-x 5 C-o | C-x 4 C-o | Display buffer in other window | | C-x 5 f | C-x 4 f | Find file in other window | | C-x 5 r | C-x 4 r | Find file read-only in other window | | C-x 5 d | C-x 4 d | Run Dired in other window | | C-x 5 . | C-x 4 . | Find tag in other window |

| C-x ^ || Grow window taller | | C-x { || Shrink window narrower | | C-x } || Grow window wider | {.shortcuts}

Formatting

--
TABIndent current line (mode-dependent)
C-M-\Indent region (mode-dependent)
C-M-qIndent sexp (mode-dependent)
C-x TABIndent region rigidly arg columns
M-;Indent for comment
C-oInsert newline after point
C-M-oMove rest of line vertically down
C-x C-oDelete blank lines around point
M-^Join line with previous (with arg, next)
M-\Delete all white space around point
M-SPCPut exactly one space at point
M-qFill paragraph
C-x fSet fill column to arg
C-x .Set prefix each line starts with
M-oSet face
{.shortcuts}

Info {.row-span-2}

--
C-h iEnter the Info documentation reader
C-h SFind specified function or variable in Info
{.shortcuts}

Moving within a node

--
SPCScroll forward
DELScroll reverse
bBeginning of node
{.shortcuts}

Moving between nodes

--
nNext node
pPrevious node
uMove up
mSelect menu item by name
nSelect nth menu item by number (1–9)
fFollow cross reference (return with l)
lReturn to last node you saw
dReturn to directory node
tGo to top node of Info file
gGo to any node by name
{.shortcuts}

Other

--
hRun Info tutorial
iLook up a subject in the indices
sSearch nodes for regexp
qQuit Info
{.shortcuts}

Minibuffer

The following keys are defined in the minibuffer.

--
TABComplete as much as possible
SPCComplete up to one word
RETComplete and execute
?Show possible completions
M-pFetch previous minibuffer input
M-nFetch later minibuffer input or default
M-rRegexp search backward through history
M-sRegexp search forward through history
C-gAbort command
{.shortcuts}

Type C-x ESC ESC to edit and repeat the last command that used the minibuffer. Type F10 to activate menu bar items on text terminals.

Tags

--
M-.Find a tag (a definition)
C-u M-.Find next occurrence of tag
M-x visit-tags-tableSpecify a new tags file
M-x tags-searchRegexp search on all files in tags table
M-x tags-query-replaceRun query-replace on all the files
M-,Continue last tags search or query-replace
{.shortcuts}

Rectangles

--
C-x r rCopy rectangle to register
C-x r kKill rectangle
C-x r yYank rectangle
C-x r oOpen rectangle, shifting text right
C-x r cBlank out rectangle
C-x r tPrefix each line with a string
{.shortcuts}

Keyboard Macros

--
C-x (Start defining a keyboard macro
C-x )End keyboard macro definition
C-x eExecute last-defined keyboard macro
C-u C-x (Append to last keyboard macro
M-x name-last-kbd-macroName last keyboard macro
M-x insert-kbd-macroInsert Lisp definition in buffer
{.shortcuts}

Buffers

--
C-x bSelect another buffer
C-x C-bList all buffers
C-x kKill a buffer
{.shortcuts}

Emacs Search{.cols-3}

Regex (common) {.row-span-2}

--
. (dot)Any single character except a newline
*Zero or more repeats
+One or more repeats
?Zero or one repeat
\Quote special characters
\cQuote regular expression special character c
|Alternative (“or”)
\(...\)Grouping
\(:?...\)Shy grouping
\(:NUM...\)Explicit numbered grouping
\nSame text as nth group
\bAt word break
\BNot at word break

Regex (entry)

StartEndEntity
^$Line
\<\>Word
\_<\_>Symbol
\‘\’Buffer
{.show-header}

Regex (conflict)

TheseOthersclass
[...][^...]Explicit set
\w\WWord-syntax character
\sc\ScCharacter with syntax c
\cc\CcCharacter with category c
{.show-header}

Incremental Search

--
C-sSearch forward
C-rSearch backward
C-M-sRegular expression search
C-M-rReverse regular expression search
M-pSelect previous search string
M-nSelect next later search string
RETExit incremental search
DELUndo effect of last character
C-gAbort current search
{.shortcuts}

Use C-s or C-r again to repeat the search in either direction. If Emacs is still searching, C-g cancels only the part not matched.

Query Replace

--
M-%Interactively replace a text string
M-x regexpUsing regular expressions
SPC / yReplace this one, go on to next
,Replace this one, don’t move
DEL / nSkip to next without replacing
!Replace all remaining matches
^Back up to the previous match
RETExit query-replace
C-rEnter recursive edit (C-M-c to exit)
{.shortcuts}

Misc

Shells

--
M-!Execute a shell command
M-&Execute a shell command asynchronously
M-Run a shell command on the region
C-u M-Filter region through a shell command
M-x shellStart a shell in window shell
{.shortcuts}

International Character Sets {.col-span-2}

--
C-x RET lspecify principal language
M-x list-input-methodsshow all input methods
C-\enable or disable input method
C-x RET cset coding system for next command
M-x list-coding-systemsshow all coding systems
M-x prefer-coding-systemchoose preferred coding system
{.shortcuts}

Registers

--
C-x r sSave region in register
C-x r iInsert register contents into buffer
C-x r SPCSave value of point in register
C-x r jJump to point saved in register
{.shortcuts}

Miscellaneous

--
C-u numNumeric argument
M--Negative argument
C-q charQuoted insert
{.shortcuts}

Commands Dealing with Emacs Lisp

--
C-x C-eEval sexp before point
C-M-xEval current defun
M-x eval-regionEval region
M-:Read and eval minibuffer
M-x load-libraryLoad a Lisp library from load-path
{.shortcuts}

Simple Customization

--
M-x customizecustomize variables and faces

Making global key bindings in Emacs Lisp:

(global-set-key (kbd "C-c g") ’search-forward)
(global-set-key (kbd "M-#") ’query-replace-regexp)

Abbrevs

--
C-x a gAdd global abbrev
C-x a lAdd mode-local abbrev
C-x a i gAdd global expansion for this abbrev
C-x a i lAdd mode-local expansion for this abbrev
C-x a eExplicitly expand abbrev
M-/Expand previous word dynamically
{.shortcuts}

Spelling Check

--
M-$Check spelling of current word
M-x ispell-regionCheck spelling of all words in region
M-x ispell-bufferCheck spelling of entire buffer
M-x flyspell-modeToggle on-the-fly spell checking
{.shortcuts}

Writing Commands {.col-span-2}

Syntax

(defun command-name (args)
"documentation" (interactive "template")
body)

Example

(defun this-line-to-top-of-window (line)
    "Reposition current line to top of window.
With prefix argument LINE, put point on LINE."
    (interactive "P")
    (recenter (if (null line)
                  0
              (prefix-numeric-value line))))

The interactive spec says how to read arguments interactively. Type C-h f interactive RET for more details.