Step 3 of 5

17. Macros

What’s in a name?

I mentioned in the previous sections that qa in normal mode starts recording a macro called a. This isn’t strictly the truth.

Vim doesn’t have a special “data store” for macros. It actually re-uses the registers that we already know about! What I mean by this is: when you start recording a macro using something like qa, Vim actually starts recording your macro into the "a register. Once you’ve finished recording, the "a register will contain the keystrokes that you used.

So really, when you type qx, you’re actually telling Vim: “start recording a macro into the "x register”

Let’s prove it in the editor. Work through the checklist below. Record a tiny macro into the "x register with qx, then list the register with :reg x. You’ll notice that it contains precisely the keystrokes that you typed while recording — even the final Esc, which Vim displays as ^[.

  1. Start recording into the x register Press qx — Vim will show recording @x in the bottom-left.
  2. Append an exclamation mark while recording This sentence could use one — press A!, then Esc.
  3. Stop recording Press q.
  4. Inspect the x register Type :reg x and hit Enter — the register holds A!^[, your exact keystrokes. Hit Enter again to dismiss the list.
Start recording into the x register Press qx — Vim will show recording @x in the bottom-left.

Next: Append an exclamation mark while recording

Loading editor…