Emacs Quick Dev Reference

Emacs is the extensible, customizable, self-documenting real time display text editor. This reference was made.

Getting started

Starting Emacs

To enter Emacs, just type its name:

$ emacs

C-z β€” Suspend Emacs

C-x C-c β€” Exit Emacs permanently

Global Description

C-<key> β€” Means hold the control, and press <key>

M-<key> β€” Means press the Esc once, and press <key>

NOTICE: This cheatsheet follows the above rules.

Motion

C-b β€” C-f β€” Haracter

M-b β€” M-f β€” Word

C-p β€” C-n β€” Line

C-a β€” C-e β€” Line beginning(or end)

M-a β€” M-e β€” Sentence

M-{ β€” M-} β€” Paragraph

C-x [ β€” C-x ] β€” Page

C-M-b β€” C-M-f β€” Sexp

C-M-a β€” C-M-e β€” Function

M-< β€” M-> β€” Buffer beginning(or end)

Case Change

M-u β€” Uppercase word

M-l β€” Lowercase word

M-c β€” Capitalize word

C-x C-u β€” Uppercase region

C-x C-l β€” Lowercase region

Files

C-x C-f β€” Read a file into Emacs

C-x C-s β€” Save a file back to disk

C-x s β€” Save all files

C-x i β€” Insert contents of another file into this buffer

C-x C-v β€” Replace this file with your file

C-x C-w β€” Write buffer to a specified file

C-x C-q β€” Toggle read-only status of buffer

Error Recovery

C-g β€” Abort partially typed or executing command

M-x recover-session β€” Recover files lost by a system crash

C-x uC-_C-/ β€” Undo an unwanted change

M-x revert-buffer β€” Restore a buffer to its original contents

C-l β€” Redraw garbaged screen

Transposing

C-t β€” Transpose characters

M-t β€” Transpose words

C-x C-t β€” Transpose lines

C-M-t β€” Transpose sexps

C-v β€” Scroll to next screen

M-v β€” Scroll to previous screen

C-x < β€” Scroll left

C-x > β€” Scroll right

C-l β€” Scroll current line to center, top, bottom

M-g g β€” Goto line

M-g c β€” Goto char

M-m β€” Back to indentation

Marking

C-@C-SPC β€” Uet mark here

C-x C-x β€” Exchange point and mark

M-@ β€” Set mark arg words away

M-h β€” Mark paragraph

C-x C-p β€” Mark page

C-M-@ β€” Mark sexp

C-M-h β€” Mark function

C-x h β€” Mark entire buffer

Killing and Deleting

DEL β€” C-d β€” Character (delete)

M-DEL β€” M-d β€” Word

M-0 C-k β€” C-k β€” Line (to end of)

C-x DEL β€” M-k β€” Sentence

M-- C-M-k β€” C-M-k β€” Sexp

C-W β€” Kill region C-w

M-w β€” Copy region to kill ring

M-z char β€” Kill through next occurrence of char

C-y β€” Yank back last thing killed

M-y β€” Replace last yank with previous kill

Getting Help

C-x 1 β€” Remove help window

C-M-v β€” Scroll help window

C-h a β€” Apropos: show commands matching a string

C-h k β€” Describe the function a key runs

C-h f β€” Describe a function

C-h m β€” Get mode-specific information

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

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

C-x 5 1 β€” C-x 1 β€” Delete all other windows

C-x 5 2 β€” C-x 2 β€” Split window, above and below

C-x 5 0 β€” C-x 0 β€” Delete 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

Formatting

TAB β€” Indent current line (mode-dependent)

C-M-\ β€” Indent region (mode-dependent)

C-M-q β€” Indent sexp (mode-dependent)

C-x TAB β€” Indent region rigidly arg columns

M-; β€” Indent for comment

C-o β€” Insert newline after point

C-M-o β€” Move rest of line vertically down

C-x C-o β€” Delete blank lines around point

M-^ β€” Join line with previous (with arg, next)

M-\ β€” Delete all white space around point

M-SPC β€” Put exactly one space at point

M-q β€” Fill paragraph

C-x f β€” Set fill column to arg

C-x . β€” Set prefix each line starts with

M-o β€” Set face

Info

C-h i β€” Enter the Info documentation reader

C-h S β€” Find specified function or variable in Info

SPC β€” Scroll forward

DEL β€” Scroll reverse

b β€” Beginning of node

n β€” Next node

p β€” Previous node

u β€” Move up

m β€” Select menu item by name

n β€” Select nth menu item by number (1–9)

f β€” Follow cross reference (return with l)

l β€” Return to last node you saw

d β€” Return to directory node

t β€” Go to top node of Info file

g β€” Go to any node by name

h β€” Run Info tutorial

i β€” Look up a subject in the indices

s β€” Search nodes for regexp

q β€” Quit Info

Minibuffer

The following keys are defined in the minibuffer.

TAB β€” Complete as much as possible

SPC β€” Complete up to one word

RET β€” Complete and execute

? β€” Show possible completions

M-p β€” Fetch previous minibuffer input

M-n β€” Fetch later minibuffer input or default

M-r β€” Regexp search backward through history

M-s β€” Regexp search forward through history

C-g β€” Abort command

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-table β€” Specify a new tags file

M-x tags-search β€” Regexp search on all files in tags table

M-x tags-query-replace β€” Run query-replace on all the files

M-, β€” Continue last tags search or query-replace

Rectangles

C-x r r β€” Copy rectangle to register

C-x r k β€” Kill rectangle

C-x r y β€” Yank rectangle

C-x r o β€” Open rectangle, shifting text right

C-x r c β€” Blank out rectangle

C-x r t β€” Prefix each line with a string

Keyboard Macros

C-x ( β€” Start defining a keyboard macro

C-x ) β€” End keyboard macro definition

C-x e β€” Execute last-defined keyboard macro

C-u C-x ( β€” Append to last keyboard macro

M-x name-last-kbd-macro β€” Name last keyboard macro

M-x insert-kbd-macro β€” Insert Lisp definition in buffer

Buffers

C-x b β€” Select another buffer

C-x C-b β€” List all buffers

C-x k β€” Kill a buffer

Emacs Search

Regex (common)

. (dot) β€” Any single character except a newline

* β€” Zero or more repeats

+ β€” One or more repeats

? β€” Zero or one repeat

\ β€” Quote special characters

\c β€” Quote regular expression special character c

\| β€” Alternative (β€œor”)

\(...\) β€” Grouping

\(:?...\) β€” Shy grouping

\(:NUM...\) β€” Explicit numbered grouping

\n β€” Same text as nth group

\b β€” At word break

\B β€” Not at word break

Regex (entry)

^ β€” $ β€” Line

\< β€” \> β€” Word

\_< β€” \_> β€” Symbol

\β€˜ β€” \’ β€” Buffer

Regex (conflict)

[...] β€” [^...] β€” Explicit set

\w β€” \W β€” Word-syntax character

\sc β€” \Sc β€” Character with syntax c

\cc β€” \Cc β€” Character with category c

Incremental Search

C-s β€” Search forward

C-r β€” Search backward

C-M-s β€” Regular expression search

C-M-r β€” Reverse regular expression search

M-p β€” Select previous search string

M-n β€” Select next later search string

RET β€” Exit incremental search

DEL β€” Undo effect of last character

C-g β€” Abort current search

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 regexp β€” Using regular expressions

SPC / y β€” Replace this one, go on to next

, β€” Replace this one, don’t move

DEL / n β€” Skip to next without replacing

! β€” Replace all remaining matches

^ β€” Back up to the previous match

RET β€” Exit query-replace

C-r β€” Enter recursive edit (C-M-c to exit)

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 shell β€” Start a shell in window shell

International Character Sets

C-x RET l β€” specify principal language

M-x list-input-methods β€” show all input methods

C-\ β€” enable or disable input method

C-x RET c β€” set coding system for next command

M-x list-coding-systems β€” show all coding systems

M-x prefer-coding-system β€” choose preferred coding system

Registers

C-x r s β€” Save region in register

C-x r i β€” Insert register contents into buffer

C-x r SPC β€” Save value of point in register

C-x r j β€” Jump to point saved in register

Miscellaneous

C-u num β€” Numeric argument

M-- β€” Negative argument

C-q char β€” Quoted insert

Commands Dealing with Emacs Lisp

C-x C-e β€” Eval sexp before point

C-M-x β€” Eval current defun

M-x eval-region β€” Eval region

M-: β€” Read and eval minibuffer

M-x load-library β€” Load a Lisp library from load-path

Simple Customization

M-x customize β€” customize 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 g β€” Add global abbrev

C-x a l β€” Add mode-local abbrev

C-x a i g β€” Add global expansion for this abbrev

C-x a i l β€” Add mode-local expansion for this abbrev

C-x a e β€” Explicitly expand abbrev

M-/ β€” Expand previous word dynamically

Spelling Check

M-$ β€” Check spelling of current word

M-x ispell-region β€” Check spelling of all words in region

M-x ispell-buffer β€” Check spelling of entire buffer

M-x flyspell-mode β€” Toggle on-the-fly spell checking

Writing Commands

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

(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.