How to Record and Repeat Key Sequences in Vim

When using Vim, I find it very handy to record and automatically repeat key sequences over multiple lines. E.g. suppose you want to extract the content from HTML markup, you could use this technique to record how you extract the text from the first line (in a way that can be repeated throughout), then repeat your actions over all the other lines. Such a recording is known as a macro and here is how to use it.

First, we want to store our macro in a register, let’s say we want to store it in 1. We start recording by clicking q1. On the status bar, the text recording @1 should appear indicating that your actions are now being recorded.

Then we perform our key sequence on the first line.

Once done, press q. The recording @1 text on the status bar should disappear.

Now, to repeat the actions saved on another line, move your cursor to that line and press @1.

If you want to perform the actions over, say the next 10 lines, then simply press 10@1.

References

Macros. http://vim.wikia.com/wiki/VimTip398

Leave a Reply

Your email address will not be published. Required fields are marked *