(I previously posted on another Vim feature, sessions, which you can find here).
Macros have been the bane of my life in Vim for a little while. I didn't really know what they did, so I never used them, but sometimes I'd mistype
q and Vim would print 'recording' at the bottom and I'd panic slightly. (If you didn't know that that 'recording' message was part of the macro feature, well, now you do!).
So, when I decided to learn some more advanced features of Vim, macros were one of the things I picked. As it turns out, they're actually pretty easy:
qand another character to start recording to a buffer identified by that character (I normally use
- type the sequence of commands you want to record - starting or finishing it with commands like 0 or j (start of line/move down a line) is good, so that after your macro runs, you're ready to run it again on the next line
qagain to stop recording
- now type
@and the name of your macro buffer (e.g.
@qif you started with
@wif you started with
qw) to replay your macro
- if you want to re-run the last-used macro,
I recently wanted to add C++ const specifiers to a lot of function definitions, which was more fun to automate with a macro than do by hand -
qqA const<Esc>q defined a macro that appended ' const' to the end of the current line, and I could just hit
@@ on any line where I wanted that to happen.