(macros)
Editing

Editing commands

Beyond raw motion, Macros provides the core Emacs editing commands — region-aware where Emacs is, and each command's edits coalesce into a single undo group so they undo as a unit.

The region and the kill ring

Set the mark with C-Space, then move point; the text between mark and point is the region. Commands that say "region" act on it (or fall back sensibly when there's no active region).

Key Command Action
C-Space set-mark Start a selection
C-w kill-region Cut the region to the kill ring
M-w kill-ring-save Copy the region
C-y yank Paste the latest kill
M-y yank-pop Cycle through earlier kills (after a yank)
C-x C-x exchange-point-and-mark Swap point and mark
C-x C-h / cmd-a mark-whole-buffer Select all

The mark ring

Every C-Space also pushes the mark onto the buffer's mark ring (the last 16 marks), so you can jump back to places you've been. M-<, M-> and finishing an isearch push the mark too, without starting a selection.

Key Command Action
C-u C-Space pop-to-mark-command Jump to the mark, then to each older one
C-x C-Space pop-global-mark Jump to the last mark set in another buffer

After C-u C-Space, each further plain C-Space pops again, so C-u C-Space C-Space C-Space walks three marks back. The ring rotates: past the oldest mark you come back to the newest. Marks move with the text when you edit around them.

The global mark ring keeps one entry per buffer change: setting a mark in a different buffer from the last one adds an entry. C-x C-Space switches to that buffer and position, and repeating it walks further back across buffers.

The kill ring and the system clipboard are kept in sync by the clipboard commands (cmd-c/x/v), so copying in Macros and pasting in another app just works.

The same keys paste into a minibuffer prompt: cmd-v inserts the clipboard, and C-y inserts the latest kill (taking in anything copied elsewhere first). A prompt is one line, so a trailing newline is dropped and only the first line of a multi-line paste goes in. In a password prompt both read the clipboard directly and never put the secret on the kill ring; see Passwords & secrets.

Killing & transposing

Key Command
C-k kill-line
C-S-Backspace kill-whole-line — the whole line and its newline, wherever point is
M-d / M-Backspace kill-word / backward-kill-word
M-z zap-to-char
C-t transpose-chars
C-x C-t transpose-lines
M-^ join-line
C-o open-line

Rectangles

A rectangle is the block of text between mark and point taken as two corners: every line from the mark's line to point's, and the columns between the mark's column and point's. Set the mark (C-Space), move to the opposite corner, then run a command. Columns are display columns, as in Emacs: a tab counts up to its next tab stop (the tab-width option) and a wide CJK or emoji character counts two. When an edge cuts through a tab or wide character, that character is split into spaces.

Key Command Action
C-x r k kill-rectangle Delete the rectangle, saving it as the last killed rectangle
C-x r M-w copy-rectangle-as-kill Save the rectangle without deleting it
C-x r d delete-rectangle Delete the rectangle without saving it
C-x r y yank-rectangle Insert the last killed rectangle with its top-left corner at point
C-x r o open-rectangle Insert blank space filling the rectangle, pushing text right
C-x r c clear-rectangle Replace the rectangle with spaces
C-x r t string-rectangle Replace each line of the rectangle with a string from the minibuffer
C-x r N rectangle-number-lines Number the lines from 1 at the rectangle's left edge
C-x SPC rectangle-mark-mode Show the region as a column block

A killed rectangle is a list of lines, and short lines are padded with spaces so every line has the rectangle's width. It also goes on the clipboard as newline-joined text. C-x r y pads short lines out to point's column and adds lines at the end of the buffer if it needs them. A rectangle of width zero still works: C-x r t then inserts the string on every line, and C-x r N numbers them. As in Emacs, C-x r o and C-x r c leave alone any line that ends before the rectangle's left edge. Each command undoes as one step.

Rectangle mark mode. C-x SPC sets the mark if none is active and highlights the region as a column block (the same highlight as evil's C-v block selection). While it's on, C-w kills the rectangle, M-w copies it and C-t runs string-rectangle. C-g or a second C-x SPC turns it off.

Registers

A register is a slot named by one character that holds text, a number, a rectangle, a position or a window layout (Emacs register.el). Each command prompts for the character. These are separate from evil's "a registers. They last for the session and are not saved across restarts.

Key Command Action
C-x r s / C-x r x copy-to-register Copy the region into a register (with C-u, delete it too)
C-x r i / C-x r g insert-register Insert a register's text, number or rectangle. Point stays before it (after it with C-u)
C-x r r copy-rectangle-to-register Copy the region's rectangle into a register (with C-u, delete it too)
C-x r SPC point-to-register Remember point
C-x r j jump-to-register Jump to a remembered position, or restore a window layout
C-x r n number-to-register Store a number: the prefix argument, otherwise the number at point (and move past it), otherwise 0
C-x r + increment-register Add the prefix argument (default 1) to a number register. For a text register, append the region to it
C-x r w window-configuration-to-register Remember the window split layout
M-x view-register Describe one register in the echo area
M-x list-registers List every register in a *Register List* buffer (q closes it)

A position register follows its text as you edit, even while another buffer is current, because it holds a marker bound to its buffer. If that buffer has been killed, C-x r j visits its file again at the stored position. A register used for counting goes like this: C-u 1 C-x r n a to start at 1, C-x r i a to insert the number, C-x r + a to step it.

Scheme can use these directly: (get-register "a") and (set-register "a" (cons "text" "hi")) read and write registers, (rectangle-bounds) gives the region's rectangle as (top bottom left right), (rectangle-extract top bottom left right) returns its lines and (insert-rectangle lines) inserts lines at point.

Sexps and defuns

Emacs's balanced-expression motion works in every buffer, not just Lisp. Lisp buffers read as Lisp (; comments, ' quote prefixes, #\( char literals). Every other language reads as bracket-balanced code: a sexp is a (…), […] or {…} group, an identifier or number, or a string literal, and operators and other punctuation are skipped. Strings and comments come from the buffer's tree-sitter tree, so a bracket inside "…" or // … never unbalances anything. Without a tree, "…", 'c', // and /* */ are recognised.

Key Command Action
C-M-f / C-M-b forward-sexp / backward-sexp Move over a sexp
C-M-n / C-M-p forward-list / backward-list Move over the next / previous bracketed group, skipping atoms
C-M-u backward-up-list Move out to the start of the enclosing group
C-M-d down-list Move into the next group
C-M-k kill-sexp Kill the sexp after point
C-M-t transpose-sexps Swap the sexps before and after point
C-M-SPC / C-M-@ mark-sexp Mark the next sexp; repeat to extend the region by another
C-M-a / C-M-e beginning-of-defun / end-of-defun To the start of this (or the previous) defun / the end of this (or the next) one
C-M-h mark-defun Mark the defun around point

up-list (out past the closing bracket) is on M-x.

A defun is a top-level form in Lisp. In other languages it is a named function or method node from the tree-sitter tree. Lambdas and closures don't count, so C-M-a finds the enclosing named function. In a buffer with no tree, a defun is a top-level {…} block, starting at the line that opens it. To add node kinds for a language, extend the list in your init.scm:

(set! *defun-node-kinds* (cons "rule_definition" *defun-node-kinds*))

The paredit edits (slurp, barf, wrap, raise, splice) remain Lisp-only. See Structural editing.

Whitespace, lines and alignment

Key Command Action
M-SPC just-one-space Collapse the spaces around point to one
M-\ delete-horizontal-space Delete all spaces and tabs around point
C-x C-o delete-blank-lines Collapse blank lines around point
C-x TAB indent-rigidly Shift the region's lines (or the current line) left or right
M-x delete-trailing-whitespace Strip trailing blanks in the region, or in the whole buffer along with blank lines at its end
M-x align-regexp Line up the first match of a regexp on each line of the region (or paragraph)
M-x keep-lines / flush-lines Keep / delete the lines matching a regexp
M-x delete-duplicate-lines Delete every repeat of an earlier line, keeping the first

After C-x TAB, ← / → move the lines one column left or right, and S-← / S-→ move them four. Any other key stops. With a prefix count it shifts by that many columns at once (C-u 8 C-x TAB). Blank lines are left alone.

keep-lines and flush-lines act on the region, or on the lines from point to the end of the buffer. As with search, an all-lower-case regexp ignores case. delete-duplicate-lines acts on the region, or on the whole buffer. align-regexp adds spaces before each match so all the matches start one column past the longest text before them. For example, aligning = turns a = 1 / bbb = 2 into a = 1 / bbb = 2.

Counting and position

Key Command Action
M-= count-words-region Lines, words and characters in the region (the buffer without one)
M-x count-words The same for the whole buffer
C-x = what-cursor-position The character after point (decimal, octal, hex, U+), point, percentage, and column

Dynamic abbreviations

M-/ (dabbrev-expand) completes the word before point from words already in your buffers. Candidates come from this buffer first, nearest match before point first and then after it, and then from the other open file buffers (their saved text). Press M-/ again straight away to replace the expansion with the next candidate. After the last one, the text you typed comes back.

Inserting characters

C-x 8 RET (insert-char) inserts any character. Type a hex code point (e9, U+1F600, #x2014) and press RET, or search a built-in list of common characters by name: dashes, quotes, arrows, math symbols, Greek letters, currency signs. The build has no full Unicode name table, so names outside that list need their code point.

Incremental search, Emacs-style:

Key Command
C-s / C-r isearch-forward / isearch-backward — a literal string
C-M-s / C-M-r isearch-forward-regexp / isearch-backward-regexp — a regexp
M-s . isearch-forward-symbol-at-point — the symbol under point, whole symbols only

Type to extend the match; C-s / C-r again to jump to the next/previous hit; RET to stop on the match (the mark is left where you started, so C-u C-Space takes you back); C-g to cancel and return to where you started. M-r while searching toggles between a literal and a regexp search. C-s C-s on an empty search reuses the previous string.

Regexps use Rust regex syntax and are case-sensitive; start one with (?i) to ignore case. A symbol search treats letters, digits and _ as symbol characters, plus -, ?, ! and friends in Lisp buffers, so M-s . on foo skips foobar and foo-bar.

Replace

Key Command
M-% query-replace — ask at each match of a literal string
C-M-% query-replace-regexp — same, for a regexp
M-x replace-string / replace-regexp replace every match without asking
C-c p r / C-c p % projectile-replace / projectile-replace-regexp — across the project

All of them run from point to the end of the buffer, or inside the active region (an evil Visual selection counts). An empty search string reuses the previous pair. An all-lower-case search matches any case, and a lower-case replacement then follows the match's case (FOO → BAR, Foo → Bar). In the regexp versions \1…\9 insert capture groups, \& the whole match, \n / \t a newline / tab.

At each match of a query session:

Key Action
y / SPC replace and go on
n / DEL skip
! replace all remaining
. replace this one and stop
, replace, but stay to look at the result
u / U undo the last / every replacement
^ back to the previous match
E edit the replacement string
q / RET stop (any other key stops too)
? help

The whole session is a single undo step. The keys work the same under evil and helix (M-% falls through the modal keymaps).

The project versions find the files with ripgrep (gitignore-aware), then run a query session through each file in turn: ! finishes the current file, Y replaces everything in all remaining files, N skips to the next file. At the end you're asked whether to save the files that changed. With evil, bind them on your leader, e.g. (evil-leader-set "n R" 'projectile-replace).

Occur

C-c o (occur) prompts for a regexp (an all-lower-case one ignores case) and opens an *occur* window listing every matching line with its line number, matches highlighted. n / p move between hits; Enter, o or a click jumps to the match in the source; q closes.

Editing results (occur-edit / wgrep)

In *occur* and in the project grep buffer (C-c p G), press e or C-c C-p (i under evil/helix) to make the results editable. Only the text after each file:line: prefix can change — the prefixes are read-only. Then:

  • C-c C-c applies every changed line. Sources open in a buffer are edited in that buffer and left modified for you to review and save; files that aren't open are patched directly on disk. A line that changed since the search is skipped and reported.
  • C-c C-k throws the edits away.

Narrowing

Narrowing restricts a buffer to part of its text, so you can work on one function or one Org subtree without the rest in the way.

Key Command Action
C-x n n narrow-to-region Narrow to the region
C-x n d narrow-to-defun Narrow to the function, method or type around point (found with tree-sitter; in Lisps, the top-level form)
C-x n s org-narrow-to-subtree In Org, narrow to the subtree at point
C-x n w widen Show the whole buffer again

While a buffer is narrowed, the mode line shows Narrow after the mode names, and:

  • Only the accessible part is shown. Line numbers count from its first line.
  • Motion stops at its edges, including M-< / M-> (beginning-of-buffer / end-of-buffer).
  • Isearch, / search, query-replace and occur only find matches inside it.
  • Edits can't reach outside it. Deleting past either edge is refused with "Beginning of buffer" or "End of buffer". Text typed at the end extends the narrowing, and the edges move with your edits, including undo.
  • Saving still writes the whole file. A formatter that rewrites the whole buffer keeps the narrowing around the same text.

From Scheme, (narrow-to-region start end) narrows to explicit byte offsets. (point-min) and (point-max) return the accessible range, (buffer-narrowed?) reports whether the buffer is narrowed, and (buffer-size) is always the whole buffer's length. As in Emacs, offsets stay absolute while narrowed. To rewrite a whole buffer, replace 0 to (buffer-size), not (point-max).

In a grid buffer, C-x n n and C-x n w keep their grid meaning (grid-narrow-to-selection / grid-widen; see Grid).

Comment toggling

M-; (comment-dwim) toggles a line comment on the current line (or region). The comment prefix is a buffer-local string set per mode (comment-string), defaulting to // ; modes like steel-mode set their own.

Sorting & case

These act on the region, or the whole buffer when there's no region:

  • sort-lines — sort the lines.
  • upcase-region / downcase-region / capitalize-region — case conversion.

Run them by name with M-x, or bind them in your init.scm.

Typing

Key Command Action
Insert overwrite-mode Toggle typing over the text after point
C-q quoted-insert Insert the next key literally

In overwrite mode each typed character replaces the one after point, but never the end of the line: past it, typing inserts again. It's per buffer, and the mode line shows Ovwrt while it's on. C-q still inserts.

Quoted insert inserts whatever key comes next instead of running it: C-q C-l inserts a form feed (^L), C-q TAB a real tab whatever the indent settings, C-q C-j a newline, C-q RET a carriage return (^M). Octal digits give a character code: C-q 0 1 4 is also a form feed. Three digits insert at once; a shorter code ends at the next key, which is dropped if it's RET and runs normally otherwise (C-q 7 a inserts a bell, then a). C-u 3 C-q x inserts xxx.

Text size

Key Command Action
C-x C-+ / C-x C-= text-scale-increase Larger text in this buffer
C-x C-- text-scale-decrease Smaller text in this buffer
C-x C-0 text-scale-reset Back to the default size
cmd-= / cmd-- / cmd-0 global-text-scale-increase / -decrease / -reset Zoom everything (every buffer and the UI)

After any C-x C-+ / C-x C-- / C-x C-0, the plain keys + (or =), - and 0 keep adjusting until you press anything else, as in Emacs. Each step is a factor of 1.2, and C-u N takes N steps. The buffer scale belongs to the buffer, so every window showing it changes together. The global zoom (also C-x C-M-+ / C-x C-M-- / C-x C-M-0) moves in steps of 1.1. In a grid buffer cmd-= and C-= zoom the grid instead, and outside one C-= stays er/expand.

Repeating a command

C-x z (repeat) runs the last command again, with the same prefix argument; keep pressing z to repeat it more. Typed text counts as a command, and so does a command run from M-x. C-u N C-x z repeats it with N as its argument instead.

The other window

Key Command Action
C-M-v scroll-other-window Scroll the next window down a page
C-M-S-v scroll-other-window-down Scroll it back up

Point stays in the window you're in, so you can page through a reference beside the code you're editing.

Undo & redo

Key Command Action
C-/ undo Undo the last change
C-? redo Redo

Undo is branching (Emacs undo-tree): undoing and then making a new edit starts a branch rather than discarding the redone edits, so no work is ever lost. M-x vundo opens a read-only view of the undo tree (g refreshes, q closes); navigate the history itself with C-/ and C-?.

Expanding the selection

Grow the region outward along tree-sitter syntax boundaries — word, then expression, statement, block (Emacs expand-region). It works in any buffer with a tree-sitter grammar, no language server needed.

Key Command Action
C-= er/expand Widen the selection to the enclosing node
C-- er/contract Step back to the previous, narrower selection

In evil visual state, + and - do the same.

Multiple cursors

Edit in many places at once (Emacs mc.el). Add secondary cursors, then typed text, Enter, and Backspace are replicated at every cursor.

Key Command Action
C-c C-n mc-add-cursor-below Add a cursor on the next line
C-c C-p mc-add-cursor-above Add a cursor on the previous line
C-c C-d mc-mark-next-like-this Add a cursor at the next occurrence of the word at point
C-g mc-clear Collapse back to a single cursor

Plain motions move only the primary cursor — add the cursors first, then type or delete.

Jumping to a character

Jump anywhere on screen in a couple of keystrokes (Emacs avy / ace-jump).

Key Command Action
C-c j avy-goto-char Type a character; every visible occurrence is tagged with a hint letter — press the letter to jump

In evil normal state this is g s. C-g cancels the jump.

Files and buffers

Key Command Action
C-x C-w write-file Save the buffer under a new path (with file-name completion, like C-x C-f). The buffer then visits that file and takes its name. Asks before overwriting an existing file. Give a directory to keep the file name.
C-x i insert-file Insert a file's contents at point. Point stays before the inserted text.
C-x C-q read-only-mode Toggle whether the buffer can be changed. The mode line shows %% while it's read-only (%* if it has unsaved changes). Files you can't write open read-only.
— rename-buffer Give the current buffer a new name.
— find-file-at-point (ffap) Open the file or URL at point. A relative path is relative to the buffer's directory. path:LINE (or path:LINE:COL, as compilers print it) lands on that line, and a URL opens in your browser. With nothing recognizable at point it prompts like C-x C-f.

The C-x 4 prefix does the same things in another window, splitting the frame if there's only one window:

Key Command Action
C-x 4 f find-file-other-window Visit a file in the other window and select it
C-x 4 b switch-to-buffer-other-window Show a buffer in the other window and select it
C-x 4 C-o display-buffer Show a buffer in the other window, but stay where you are
C-x 4 0 kill-buffer-and-window Kill the current buffer and close its window

Saving keeps a backup of each file's original contents, and can hold Emacs-style lock files. See Backups and lock files.

In Scheme, each of these commands takes its argument directly instead of prompting: (rename-buffer "notes"), (insert-file "~/header.txt"), (find-file-other-window path), (display-buffer "*Messages*"). (write-file path) saves the buffer as path, and (write-file path text) still writes text to a file as before. That two-argument form is also available as write-file-text.

The buffer list

C-x C-b (ibuffer) opens a buffer manager (Emacs ibuffer) listing every open buffer. Enter switches to the buffer on the line, d or x kills it, g refreshes. (To pick a buffer through Helm instead, use C-x b.)