🛠️ 开发工具

Emacs

Emacs Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · 开发工具🧭 Markdown 速查🏷️ 2 个标签
#emacs#editor
向下滚动查看内容
返回全部 Cheat Sheets

Getting started

Starting Emacs

To enter Emacs, just type its name:

SHELL
滚动查看更多
$ emacs

--
C-zSuspend Emacs
C-x C-cExit 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
BackwardForwardEntity to move over
C-bC-fHaracter
M-bM-fWord
C-pC-nLine
C-aC-eLine beginning<br/>(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<br>(or end)
Case Change
--
M-uUppercase word
M-lLowercase word
M-cCapitalize word
C-x C-uUppercase region
C-x C-lLowercase region
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
Error Recovery
--
C-gAbort partially typed or executing command
M-x recover-sessionRecover files lost by a system crash
C-x u<br>C-_<br>C-/Undo an unwanted change
M-x revert-bufferRestore a buffer to its original contents
C-lRedraw garbaged screen
Transposing
--
C-tTranspose characters
M-tTranspose words
C-x C-tTranspose lines
C-M-tTranspose sexps

Scroll

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

Goto

--
M-g gGoto line
M-g cGoto char
M-mBack to indentation
Marking
--
C-@<br/>C-SPCUet 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
Killing and Deleting
BackwardForwardEntity to kill
DELC-dCharacter <br>(delete)
M-DELM-dWord
M-0 C-kC-kLine <br/> (to end of)
C-x DELM-kSentence
M-- C-M-kC-M-kSexp

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

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 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 3Split window, side by side
C-M-vScroll other window
C-x 5 oC-x oSwitch cursor to another window
C-x 5 bC-x 4 bSelect buffer in other window
C-x 5 C-oC-x 4 C-oDisplay buffer in other window
C-x 5 fC-x 4 fFind file in other window
C-x 5 rC-x 4 rFind file read-only in other window
C-x 5 dC-x 4 dRun 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
--
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
Info
--
C-h iEnter the Info documentation reader
C-h SFind specified function or variable in Info

Moving within a node

--
SPCScroll forward
DELScroll reverse
bBeginning of node

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

Other

--
hRun Info tutorial
iLook up a subject in the indices
sSearch nodes for regexp
qQuit Info
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

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
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
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
Buffers
--
C-x bSelect another buffer
C-x C-bList all buffers
C-x kKill 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
\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
Regex (conflict)
TheseOthersclass
[...][^...]Explicit set
\w\WWord-syntax character
\sc\ScCharacter with syntax c
\cc\CcCharacter with category c
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

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)

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
International Character Sets
--
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
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
Miscellaneous
--
C-u numNumeric argument
M--Negative argument
C-q charQuoted insert
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
Simple Customization
--
M-x customizecustomize variables and faces

Making global key bindings in Emacs Lisp:

CODE
滚动查看更多
(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
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
Writing Commands

Syntax

LISP
滚动查看更多
(defun command-name (args)
"documentation" (interactive "template")
body)

Example

LISP
滚动查看更多
(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.

相关 Cheat Sheets