Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 38 ยท Workflows

Keyboard Macros, Registers and Bookmarks in Emacs

Record and replay keyboard macros, run them on many lines, save text and positions in registers, and use bookmarks to return to files, with exact key sequences.

Repetitive editing is where Emacs earns its reputation. If you can do a task once by hand, you can usually record it and replay it a hundred times. Keyboard macros record your keystrokes; registers store text, numbers, rectangles and positions under a single character; bookmarks remember places across sessions. This lesson shows the keys and the thinking that makes them reliable.

All keys are real Emacs keys. The runnable blocks are JavaScript stand-ins that model macro replay and register storage.

Recording a macro

  1. F3 (or C-x () starts recording. The mode line shows Def.
  2. Perform the edit with ordinary commands.
  3. F4 (or C-x )) stops recording.
  4. F4 again (or C-x e) replays the macro once. After C-x e, you can keep pressing e to repeat.

Prefix arguments set the count: C-u 50 F4 or M-50 F4 runs it 50 times. C-u 0 F4 runs it until it fails, which is how you apply it to a whole buffer: the macro stops with an error when a search or movement can no longer proceed.

Designing a robust macro

A good macro leaves the cursor ready for the next repetition. Follow these rules:

  • Start from a known position, such as C-a (beginning of line) or a search with C-s.
  • Use movement that adapts: M-f (forward word), C-s , (search for a comma), C-a and C-n rather than counting characters.
  • End on the next starting place: finish with C-n C-a so the next run starts on the following line.
  • Avoid the mouse, and prefer searches to arrow keys inside text that varies in length.

Example task: turn lines such as alice 30 into Alice (30). Macro: C-a, M-c (capitalize the word; point ends after it), C-f (step over the space), type (, C-e, type ), then C-n C-a. Run it with C-u 0 F4 (the final C-n fails at the last line, which stops it cleanly).

JS Stand-in: record a macro and replay it

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Managing macros

A macro lives only until you record another. To keep it:

  • C-x C-k n (kmacro-name-last-macro) gives it a command name such as my-wrap-age, so you can run it with M-x.
  • M-x insert-kbd-macro RET my-wrap-age RET inserts Lisp code into the buffer; paste it into your init file to make it permanent.
  • C-x C-k b binds the last macro to a key; C-x C-k e edits it; C-x C-k C-e edits the macro as text.
  • C-x C-k C-n / C-x C-k C-p cycle through the macro ring of recent macros.
  • C-x C-k r (apply-macro-to-region-lines) applies the last macro to every line of the region, which saves you from designing for the repeat count.
  • A counter lets a macro insert numbers: F3 starts recording, C-x C-k C-i inserts the counter and increments it, so C-u 10 F4 writes 1 through 10.

Query inside a macro: C-x q while recording makes Emacs pause during replay and ask "Proceed with macro?"; answer SPC to continue, C-r to edit by hand.

ELISP
(fset 'my-wrap-age
   (kmacro-lambda-form [?\C-a ?\M-c ?\C-f ?\( ?\C-e ?\) ?\C-n ?\C-a] 0 "%d"))
(global-set-key (kbd "C-c w") #'my-wrap-age)

Registers

A register is a named slot, identified by a single character (letter, digit or symbol), that holds one thing: text, a number, a rectangle, a window configuration or a position.

Keys Action
C-x r s a Copy the region into register a
C-x r i a Insert the text of register a at point
C-x r SPC a Store the current position (point) in register a
C-x r j a Jump to the position stored in a
C-x r n a Store a number; C-x r + adds to it
C-x r r a Copy the rectangle into register a
C-x r w a Store the current window configuration; C-x r j a restores it
C-x r f a Store the frame configuration

Registers last for the session. A common pattern: store your place with C-x r SPC a, wander off to check something, then C-x r j a returns instantly. For more than a few positions use bookmarks or the mark ring (C-u C-SPC cycles previous positions; C-x C-x swaps point and mark).

JS Stand-in: a register file

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Bookmarks

Bookmarks are registers that persist. C-x r m (bookmark-set) names the current file and position; C-x r b (bookmark-jump) jumps to one by name with completion; C-x r l lists all. Emacs saves them to ~/.emacs.d/bookmarks whenever they change (bookmark-save-flag set to 1). Bookmarks work for files, directories (Dired), Info nodes and, with TRAMP, remote files.

When a macro "almost works", re-record it instead of editing it. Start from a clean first line, rehearse the keys once without recording, and keep the macro short. Combine it with query-replace (C-M-%) inside the macro for conditional edits.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which keys start and stop recording a keyboard macro?

    F3 (or C-x () starts, F4 (or C-x )) stops. F4 again replays.
  2. 2. How do you run the last macro on every remaining line until it fails?

    A zero prefix repeats until an error, such as failing to move down at the end of the buffer.
  3. 3. What does C-x r SPC a followed later by C-x r j a do?

    SPC stores the position and j jumps to it.
  4. 4. How do you keep a macro permanently?

    A macro is lost when you record another unless you name it and save its Lisp definition.

Summary

  • F3 starts, F4 stops and replays; C-u N F4 repeats N times, C-u 0 F4 until failure.
  • Make macros robust by anchoring to line starts and searches, and ending where the next run begins.
  • Name macros with C-x C-k n and save them with insert-kbd-macro.
  • Registers store text, positions, numbers and window setups under one character; bookmarks persist across sessions.
  • C-x r s/i copy and insert text, C-x r SPC/j save and jump to positions.

Next: The Emacs Server and Session Persistence