Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 38 ยท Editing and Buffers

Rectangles, Registers and Keyboard Macros

Edit columns with rectangle commands, store text and positions in registers, and record and replay keyboard macros with F3, F4, C-x e and counters.

This lesson covers three small tools that remove a surprising amount of repetitive work: rectangles for column editing, registers for named storage, and keyboard macros for recording a sequence of keys and replaying it.

Rectangles

A rectangle is a block of text defined by two corners, such as a column in a table. Set the mark at one corner, move point to the opposite corner, and the rectangle is the characters inside the columns spanned by both, on every line between.

Keys Command Effect
C-x SPC rectangle-mark-mode Show the selection as a rectangle (Emacs 24.4+)
C-x r k kill-rectangle Kill the rectangle (saved in its own rectangle ring, not the kill ring)
C-x r M-w copy-rectangle-as-kill Copy it
C-x r y yank-rectangle Insert the last killed rectangle at point
C-x r d delete-rectangle Delete without saving
C-x r o open-rectangle Insert blank space, pushing text right
C-x r c clear-rectangle Blank the area
C-x r t string-rectangle Replace the rectangle on every line with a string
C-x r N rectangle-number-lines Insert line numbers in a column

With C-x SPC, the usual kill commands (C-w, M-w, C-y) act on the rectangle. C-x r t is the star: select the first column of ten lines, press C-x r t, type // and RET, and ten lines are commented. Or type to indent them. In rectangle mode, pressing C-o opens space, and typing text replaces the rectangle.

JS Stand-in: string-rectangle on a column

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

No output.

Registers

A register is a named slot (a letter, digit or symbol) that stores text, a position, a rectangle, a window layout or a number. Registers are quick, explicit storage: you decide where to put things.

Keys Command Effect
C-x r s a copy-to-register Save the region in register a
C-x r i a insert-register Insert the contents of a
C-x r SPC a point-to-register Remember this position (file and point) in a
C-x r j a jump-to-register Go to the position in a
C-x r w a window-configuration-to-register Save the window layout
C-x r n a / C-x r + Store a number / add to it
M-x list-registers See what is stored

A typical use: you are reading a function and need to look at another file, then come back. Press C-x r SPC a, wander off, and return with C-x r j a. Or copy two different snippets into a and b and insert them separately, with no kill-ring juggling. Registers last for the session only. For positions that must survive restarts, use bookmarks: C-x r m sets one, C-x r b jumps to one, and C-x r l lists them. They are saved automatically in a file.

JS Stand-in: registers hold text and positions

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

No output.

Keyboard macros

A keyboard macro records the keys you press and replays them. Recording is the simplest form of automation in Emacs, because it needs no programming.

Keys Effect
F3 or C-x ( Start recording
F4 or C-x ) Stop recording (and, if not recording, replay the last macro)
F4 (again) Replay the last macro
C-x e Replay the last macro (then press e to repeat again)
C-u 10 C-x e Replay it 10 times
C-u 0 C-x e Replay until an error (such as the end of the buffer) stops it
C-x C-k n Give the macro a name (kmacro-name-last-macro)
C-x C-k b Bind it to a key
M-x apply-macro-to-region-lines Run it on each line of the region
C-x C-k e Edit the last macro

Writing macros that repeat well

A macro that works on one line should end positioned at the start of the next one, so that a repeat continues naturally. Move with commands that do the same thing everywhere: C-a, C-e, M-f, searches (C-s, which also fails cleanly at the end and stops the loop) instead of arrow keys that depend on column counts.

Example: you have lines like Smith, John and want John Smith.

  1. Put point at the start of the first line, press F3.
  2. Press C-a, M-d (kills the surname, leaving , John), C-d C-d (deletes the comma and the space), C-e, SPC, C-y (yanks the surname back at the end), then C-n C-a to land on the start of the next line.
  3. Press F4 to stop. The first line is fixed and point is on the next line.
  4. Press C-u 0 F4 (or C-u 0 C-x e): the macro runs on every remaining line until the end of the buffer.

Macros are also available as a counter: pressing F3 while recording (or C-x C-k C-i) inserts the macro counter, which goes up by one on every run. That is good for numbering lines.

JS Stand-in: record and replay a macro

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

No output.

Saving a macro for later

M-x insert-kbd-macro writes a named macro as Lisp so you can paste it into your init file. Naming (C-x C-k n) first is required. This bridges to the next chapter on configuration and Lisp. For heavy edits, a few lines of Elisp or M-% is clearer than a macro, but nothing beats a macro for a one-off clean-up.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which key starts recording a keyboard macro?

    F3 (or C-x () starts; F4 (or C-x )) stops and replays.
  2. 2. How do you run a macro until it hits an error such as the end of the buffer?

    A prefix argument of 0 means repeat until an error.
  3. 3. Which command inserts the same string at a column on every line of a rectangle?

    C-x r t (string-rectangle) replaces the rectangle on every line with your string.
  4. 4. How do you store the region in register a?

    C-x r s a copies the region to register a; C-x r i a inserts it.

Summary

  • Rectangles (C-x SPC, C-x r k, C-x r y, C-x r t) edit columns.
  • Registers store text, positions and layouts under a letter: C-x r s, C-x r i, C-x r SPC, C-x r j.
  • Macros record keys (F3/F4) and replay them (C-x e, C-u 0 C-x e); design them so they end ready for the next repeat.
  • Bookmarks are persistent registers for positions.

Next: Org Mode Basics: Outlines, Lists and Links