Vim From Scratch

How To Edit A Vim Macro?

> 9 months ago

I use macros (by the way this is plural of macro) pretty often in my daily workflow. There are always some tasks that you need to do quickly on a large text file, and that's where macros shine.

Sometimes though it's not trivial to get a macro right from the first time. But do you know there's a simple hack to modify an already recorded macro without re-recording it?

First, a quick recap of what macro is.

Macro is a sequence of operations you can record in Vim in order to replay it later and thus automate some repetitive work.

You may know how to record a macro. You press qa in normal mode. Then make some operations and press q again to stop recording. Now you recorded your macro. In order to replay it, you can press @a and then @@ to replay the last macro. Then you can obviously run it 100 times with 100@@.

Now in this case, when you press qa , a is a Vim register. The same register that we use for yanking and pasting. That means that after recording a macro, you can see what's inside by typing :registers. Or by simply pasting from it "ap.

You will see that it is just a text. Whatever you type while recording a macro is stored in that register a simple sequence of letters typed.

Now the trick is that you can paste your macros into your file, then modify the sequence of operations and then yank it back with "ay. The macro is updated and you can run it again.

Where to go from here?

Sign Up And Get A Free eBook!

If you're interested in Vim but just don't know how to start, subscribe to this newsletter. You will get notified when the course is ready and get the latest updates and articles around Vim. Also you will get a free "Vim From Scratch" eBook!