GNU Emacs Reference
A practical guide to the Emacs operating model, commands, editing, discovery, programming tools, and configuration.
Updated 2026-07-22
Emacs Mental Model
Emacs is a programmable environment built around buffers and commands. A file is displayed through a buffer, a buffer is displayed in one or more windows, and windows belong to graphical or terminal frames.
The cursor position is called point. The mark stores another position. When point and mark delimit active text, that text is the region. Most editing commands operate at point, on the region, or on a syntactic unit determined by the active mode.
| Concept | Meaning |
|---|---|
| Frame | A top-level graphical or terminal Emacs display |
| Window | An area of a frame displaying one buffer |
| Buffer | An editable in-memory object that may or may not visit a file |
| File | Persistent data that a buffer can visit and save |
| Point | The current editing position |
| Mark | A saved position used with point to define the region |
| Region | Text between point and mark |
| Command | An interactive Emacs Lisp function |
| Mode | A keymap and behavior set active in a buffer |
Keys and Commands
Emacs documentation uses C- for Control, M- for Meta, S- for Shift, RET for Return, SPC for Space, TAB for Tab, and DEL for Backspace. Meta is normally Alt or Option; pressing Escape before a key can also enter a Meta key.
A key sequence invokes a named command. Prefix keys such as C-x, C-c, and C-h begin larger keymaps rather than immediately performing an operation. M-x invokes a command by its name, which makes every interactive command accessible even when it has no key binding.
| Input | Command | Purpose |
|---|---|---|
| M-x | execute-extended-command | Run an interactive command by name |
| C-g | keyboard-quit | Cancel the current command |
| C-u | universal-argument | Supply or build a prefix argument |
| M-5 | digit-argument | Supply numeric argument 5 |
| C-x | Control-X-prefix | Begin the general C-x command map |
| C-h | help-command | Begin the help command map |
Major and Minor Modes
Every buffer has one major mode that defines its primary editing behavior. A buffer can also have any number of minor modes that add focused features such as automatic filling, syntax checking, or visual line wrapping.
Modes contribute key bindings, menus, syntax rules, indentation, font locking, and hooks. The mode line shows the active major mode and usually abbreviates active minor modes.
| Command | Purpose |
|---|---|
| C-h m | Describe the active major and minor modes |
| M-x normal-mode | Recompute the appropriate major and local modes |
| M-x fundamental-mode | Use the minimal built-in major mode |
| M-x text-mode | Use the general mode for human-readable text |
| M-x auto-fill-mode | Toggle automatic line filling |
| M-x display-line-numbers-mode | Toggle line numbers in the buffer |
;; Run a function whenever text-mode starts.
(add-hook 'text-mode-hook #'visual-line-mode) Help and Discovery
Emacs is designed to explain its current state. Help commands inspect the exact running command, variable, keymap, and mode rather than requiring you to memorize a static configuration.
When a key behaves unexpectedly, describe the key and inspect the active modes. When you know the desired action but not its name, use apropos or search the Info manual.
| Key | Command | Purpose |
|---|---|---|
| C-h k KEY | describe-key | Describe the command bound to KEY |
| C-h c KEY | describe-key-briefly | Show only the command name |
| C-h f NAME | describe-function | Describe a Lisp function |
| C-h v NAME | describe-variable | Describe a variable and its value |
| C-h x NAME | describe-command | Describe an interactive command |
| C-h m | describe-mode | Describe active modes and local bindings |
| C-h b | describe-bindings | List all active bindings |
| C-h a TOPIC | apropos-command | Find matching commands |
| C-h w NAME | where-is | Find keys bound to a command |
| C-h l | view-lossage | Show recent input events |
| C-h i | info | Open the Info documentation browser |
| C-h r | info-emacs-manual | Open the Emacs manual |
Files and Buffers
Visiting a file creates or reuses a buffer whose contents correspond to that file. Editing changes the buffer; saving writes those changes to storage. Non-file buffers hold help, process output, messages, directories, or temporary text.
Buffer names and file names are related but distinct. Two files with the same base name receive distinct buffer names, and a buffer can be renamed without renaming its file.
| Key | Command | Purpose |
|---|---|---|
| C-x C-f | find-file | Visit or create a file |
| C-x C-s | save-buffer | Save the current buffer |
| C-x s | save-some-buffers | Offer to save modified buffers |
| C-x C-w | write-file | Write the buffer to another file |
| C-x b | switch-to-buffer | Switch buffers |
| C-x C-b | list-buffers | Display the buffer list |
| C-x k | kill-buffer | Kill a buffer |
| M-x revert-buffer | revert-buffer | Replace the buffer with its stored contents |
| M-x rename-buffer | rename-buffer | Change the buffer name only |
Windows, Frames, and Tabs
An Emacs window is a pane inside a frame, not an operating-system window. Multiple windows can show the same buffer at different positions, and display commands choose where buffers appear without changing the buffer itself.
Frames are top-level displays. Tab bars store reusable window configurations within a frame; they do not replace buffers.
| Key | Command | Purpose |
|---|---|---|
| C-x 0 | delete-window | Delete the selected window |
| C-x 1 | delete-other-windows | Keep only the selected window |
| C-x 2 | split-window-below | Split below |
| C-x 3 | split-window-right | Split to the right |
| C-x o | other-window | Select another window |
| C-x 4 f | find-file-other-window | Visit a file in another window |
| C-x 5 2 | make-frame-command | Create a frame |
| C-x 5 0 | delete-frame | Delete the selected frame |
| C-x t 2 | tab-new | Create a tab-bar tab |
| C-x t o | tab-next | Select the next tab |
Editing, Killing, Yanking, and Undo
Deletion removes text without normally saving it. Killing removes text and records it in the kill ring. Consecutive kill commands can append to one kill-ring entry, allowing larger units to be assembled and yanked later.
Yanking inserts a kill-ring entry. Immediately following a yank with M-y rotates through older entries. Emacs undo records buffer changes as a sequence and can itself be interrupted and traversed.
| Key | Command | Purpose |
|---|---|---|
| DEL / C-d | delete-backward-char / delete-char | Delete a character |
| M-DEL / M-d | backward-kill-word / kill-word | Kill a word |
| C-k | kill-line | Kill to the end of the line |
| C-w | kill-region | Kill the region |
| M-w | kill-ring-save | Copy the region to the kill ring |
| C-y | yank | Insert the newest kill |
| M-y | yank-pop | Replace the preceding yank with an older kill |
| C-x u / C-_ / C-/ | undo | Undo changes |
| M-q | fill-paragraph | Reflow a paragraph |
| M-; | comment-dwim | Insert, align, or remove a comment |
Point, Mark, Region, and Narrowing
C-SPC records the current point as the mark. Moving point then creates a region. Transient Mark mode visually highlights active regions and lets region-aware commands distinguish active selections from inactive marks.
Emacs keeps a mark ring, so previous marks can be revisited. Narrowing temporarily restricts editing and display to part of a buffer; widening restores the accessible portion.
| Key | Command | Purpose |
|---|---|---|
| C-SPC | set-mark-command | Set or activate the mark |
| C-x C-x | exchange-point-and-mark | Exchange point and mark |
| C-u C-SPC | set-mark-command | Jump through earlier marks |
| C-x h | mark-whole-buffer | Select the whole buffer |
| M-h | mark-paragraph | Select a paragraph |
| C-M-h | mark-defun | Select a definition |
| C-x n n | narrow-to-region | Restrict access to the region |
| C-x n w | widen | Restore the full buffer |
Search, Replace, and Regular Expressions
Incremental search updates the match as the search string is typed. Repeating the search key moves to another match, RET accepts the location, and C-g backs out or cancels.
Query replace asks before each replacement. Regular-expression variants use Emacs regular-expression syntax, whose escaping can differ from PCRE, JavaScript, and shell tools.
| Key | Command | Purpose |
|---|---|---|
| C-s / C-r | isearch-forward / isearch-backward | Incremental literal search |
| C-M-s / C-M-r | isearch-forward-regexp / isearch-backward-regexp | Incremental regexp search |
| M-% | query-replace | Confirm replacements of a string |
| C-M-% | query-replace-regexp | Confirm regexp replacements |
| M-s o | occur | List lines matching a regexp |
| M-x grep | grep | Run grep and make results navigable |
| M-g n / M-g p | next-error / previous-error | Move through result locations |
| Query key | Effect |
|---|---|
| y / SPC | Replace this match |
| n / DEL | Skip this match |
| ! | Replace all remaining matches |
| . | Replace this match and exit |
| q / RET | Exit query replace |
| C-r | Enter a recursive edit at this match |
Minibuffer, Completion, and History
The minibuffer reads command arguments such as file names, buffers, commands, variables, and search patterns. Its completion category determines which candidates and matching rules apply.
Minibuffer histories are usually separate by argument type. Reusing history is often faster and safer than retyping a path or command.
| Key | Purpose |
|---|---|
| TAB | Complete as far as possible or show candidates |
| SPC | Complete a word when the active completion permits it |
| ? | Display completion candidates |
| M-p / M-n | Move backward or forward through history |
| M-r | Search minibuffer history with a regexp |
| C-r / C-s | Search history incrementally |
| RET | Submit the current input |
| C-g | Abort the minibuffer command |
Registers and Rectangles
Registers are named single-character slots that can store text, positions, numbers, rectangles, window configurations, and other values. They are useful for deliberate temporary storage outside the kill ring.
Rectangle commands treat a region as columns spanning multiple lines. Rectangle Mark mode provides interactive rectangular selection and editing.
| Key | Command | Purpose |
|---|---|---|
| C-x r s r | copy-to-register | Copy region text to register r |
| C-x r i r | insert-register | Insert register r |
| C-x r SPC r | point-to-register | Save the current position |
| C-x r j r | jump-to-register | Jump to a stored position or configuration |
| C-x SPC | rectangle-mark-mode | Start a rectangular region |
| C-x r k | kill-rectangle | Kill the selected rectangle |
| C-x r y | yank-rectangle | Insert the last killed rectangle |
| C-x r t | string-rectangle | Replace each rectangle row with text |
Dired
Dired is Emacs’s directory editor. A Dired buffer lists files and lets commands operate on the file at point, marked files, or files selected by a prefix argument.
Marking and flagging are different: marks select files for general commands, while deletion flags queue files for removal by x.
| Key | Command | Purpose |
|---|---|---|
| C-x d | dired | Open a directory |
| n / p | dired-next-line / dired-previous-line | Move between entries |
| RET / ^ | dired-find-file / dired-up-directory | Visit an entry or parent |
| m / u / U | dired-mark / dired-unmark / dired-unmark-all-marks | Manage marks |
| d / x | dired-flag-file-deletion / dired-do-flagged-delete | Queue and execute deletions |
| C / R | dired-do-copy / dired-do-rename | Copy or rename files |
| D | dired-do-delete | Delete selected files after confirmation |
| + | dired-create-directory | Create a directory |
| g | revert-buffer | Refresh the directory listing |
| M-x wdired-change-to-wdired-mode | wdired-change-to-wdired-mode | Edit file names as buffer text |
Programming Tools
Emacs programming modes provide syntax-aware movement, indentation, comments, and commands. Xref supplies a common interface for definitions and references, project.el identifies projects, Compilation mode navigates diagnostics, and Eglot integrates language servers.
The available backend depends on the active major mode, project files, tags, language server, and installed external tools.
| Key or command | Command | Purpose |
|---|---|---|
| M-. | xref-find-definitions | Find a definition |
| M-? | xref-find-references | Find references |
| M-, | xref-go-back | Return through xref history |
| C-M-a / C-M-e | beginning-of-defun / end-of-defun | Move by definitions |
| C-M-h | mark-defun | Mark a definition |
| C-M-\ | indent-region | Indent the region |
| M-x compile | compile | Run a build or check command |
| M-g n / M-g p | next-error / previous-error | Navigate diagnostics |
| C-x p p | project-switch-project | Select a known project |
| M-x eglot | eglot | Start or connect to a language server |
Shell and Process Integration
Emacs can invoke one-off commands, filter regions, and host interactive process buffers. shell-mode sends input through a shell process using comint, while term and ansi-term provide terminal-style character handling.
Process buffers are normal Emacs buffers with mode-specific bindings. Their working directory, environment, coding system, and process state affect command behavior.
| Key or command | Command | Purpose |
|---|---|---|
| M-! | shell-command | Run a command and display its output |
| M-& | async-shell-command | Run a command asynchronously |
| M-| | shell-command-on-region | Send the region through a command |
| M-x shell | shell | Start an interactive shell buffer |
| M-x term | term | Start a terminal-emulation buffer |
| M-x eshell | eshell | Start the Emacs Lisp-based command shell |
| C-c C-c in comint | comint-interrupt-subjob | Interrupt the active subprocess |
| C-c C-z in comint | comint-stop-subjob | Stop the active subprocess |
Customization
Emacs behavior is controlled by variables, faces, hooks, and keymaps. The Customize interface discovers options and records selected values, while an init file can express configuration directly in Emacs Lisp.
Prefer documented customization variables and public functions over changing internal implementation details. Apply mode-specific behavior through hooks and mode keymaps rather than globally when possible.
| Command | Purpose |
|---|---|
| M-x customize | Open the top-level Customize UI |
| M-x customize-group | Customize an option group |
| M-x customize-variable | Customize one variable |
| M-x customize-face | Customize a face |
| M-x set-variable | Set a variable for the current session |
| C-h v | Inspect a variable before changing it |
;; Set an option through its supported customization API.
(setq-default indent-tabs-mode nil)
;; Enable a feature in programming buffers.
(add-hook 'prog-mode-hook #'display-line-numbers-mode)
;; Bind a global key to a command.
(global-set-key (kbd "C-c f") #'find-file) Init Files and Emacs Lisp
Emacs reads a user init file during normal startup. Common locations include ~/.emacs, ~/.emacs.d/init.el, and ~/.config/emacs/init.el; the exact selected file is available in the user-init-file variable.
Configuration is Emacs Lisp. Forms can be evaluated interactively while developing, then placed in the init file after their behavior is understood.
| Key or expression | Purpose |
|---|---|
| C-x C-e | Evaluate the Lisp form before point |
| M-: | Read and evaluate a Lisp expression |
| M-x eval-buffer | Evaluate the current buffer |
| M-x load-file | Load an Emacs Lisp file |
| user-init-file | Path of the loaded user init file |
| user-emacs-directory | Base directory for user Emacs data |
;; Load optional configuration only after a feature is available.
(with-eval-after-load 'dired
(define-key dired-mode-map (kbd "C-c o") #'dired-display-file)) Packages
package.el installs and manages Emacs Lisp packages from configured archives. Emacs also ships many built-in libraries that can be loaded without installing an external package.
Archive contents are code executed with the user’s permissions. Review archive trust, package provenance, signatures, and update changes as part of normal dependency management.
| Command | Purpose |
|---|---|
| M-x list-packages | Browse available and installed packages |
| M-x package-refresh-contents | Refresh archive metadata |
| M-x package-install | Install a named package |
| M-x package-delete | Remove an installed package |
| C-h P | Describe an installed or available package |
| C-h p | Find packages by topic keyword |
;; Configure a package after it has loaded.
(with-eval-after-load 'example-package
(setq example-option t)) Server, Client, and Daemon Use
An Emacs server lets external emacsclient processes ask an existing Emacs instance to visit files or evaluate expressions. This avoids starting and initializing a new instance for each editor request.
A daemon starts without an initial graphical frame. Clients can create frames as needed. Server authentication data identifies the server and must remain protected.
| Command | Purpose |
|---|---|
| M-x server-start | Start a server in the current Emacs instance |
| emacs --daemon | Start Emacs as a background daemon |
| emacsclient FILE | Visit a file through the server |
| emacsclient -c FILE | Create a graphical client frame |
| emacsclient -t FILE | Open a terminal client frame |
| C-x # | Notify a waiting client that editing is finished |
Recovery and Troubleshooting
Start by separating configuration problems from Emacs itself. A quick startup without the user init file and site file provides a clean comparison. Messages, command descriptions, active modes, and recent input usually reveal which component changed behavior.
Autosave data can recover edits after a crash. Backup files preserve earlier saved contents. Their names and locations depend on the active autosave and backup configuration.
| Command | Purpose |
|---|---|
| emacs -Q | Start without init, site, or package initialization |
| emacs --debug-init | Show a debugger for an init-file error |
| C-h e | Display the Messages buffer |
| C-h l | Display recent input events |
| C-h k KEY | Identify the command currently bound to a key |
| C-h m | Inspect modes affecting the current buffer |
| M-x toggle-debug-on-error | Enter the debugger when a Lisp error occurs |
| M-x recover-file | Recover a file from autosave data |
| M-x recover-session | Recover files from an interrupted session |