Step 2 of 5

17. Macros

Replaying macros

Once you’ve recorded a macro, you can replay it by typing @, followed by the name of the macro, in normal mode. For example — if you record a macro called a with qa, then you can replay that macro with @a in normal mode.

It’s easier to see the magic of macros in practice, so let’s try this in the editor. Work through the checklist below: record a macro called a that puts an X at the start and the end of the cursor line, then move to the second line and replay the whole edit with @a.

Wow! Did you see that? The macro did all the hard work for us!

Now that we know the basics of how to use macros, we’ll delve into them in a bit more detail in the coming sections.

  1. Put your cursor on the first line Jump to the top of the buffer — press gg.
  2. Start recording a macro called a Press qa — Vim will show recording @a in the bottom-left.
  3. Prefix the line with an X Press IX, then Esc to return to normal mode.
  4. Append an X as well Press AX, then Esc again.
  5. Stop recording Press q — the recording @a flag will disappear.
  6. Move to the second line Press j.
  7. Replay the macro Press @a and watch both edits happen in one go.
Put your cursor on the first line Jump to the top of the buffer — press gg.

Next: Start recording a macro called a

Loading editor…