How To Edit A Vim Macro?

October 27, 2018

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?

Vim For Developers

Vim For Developers

❄️ 35% off this Christmas season! ❄️

Learn Vim and upgrade your productivity to the next level by building the IDE of your dreams.

LEARN MORE