Step 4 of 5

17. Macros

You don’t even have to record!

Since “macros” are really just keystrokes contained within a register, you don’t even have to use Vim’s recording functionality to start recording a macro. It mainly exists as a convenience function — and it’s definitely what you want to be using most of the time — but it isn’t strictly necessary!

Instead, you can type out the macro steps on a line, then yank that line into a register of your choosing — say "x. You can then execute the contents of the "x register with @x. If you do this, you will have created and executed a macro without ever using Vim’s recording functionality.

Try it in the editor. Work through the checklist below. Write the keystrokes A!!! on a line of their own, yank that line into the "x register with "xyy, then move back to the headline and execute the register with @x. Amazing!

One quirk to be aware of: yy yanks the line’s trailing line break too, so the replay ends with a press of Enter. The macro is in insert mode at that point, so the Enter opens a fresh line and leaves you in insert mode — just hit Esc to settle back into normal mode. (If that bothers you, yank the keystrokes without the line break — e.g. 0"xy$.)

  1. Write the macro out as plain text Open a line below with o, type the three keystrokes A!!!, and press Esc.
  2. Yank the line into the x register With your cursor still on the A!!! line, type "xyy.
  3. Jump back to the headline Press gg.
  4. Execute the register Press @x — the line's keystrokes replay onto the headline — then press Esc to get back to normal mode.
Write the macro out as plain text Open a line below with o, type the three keystrokes A!!!, and press Esc.

Next: Yank the line into the x register

Loading editor…