TAG | editors
Vim Macros
I love vim macros, they are so very useful. How do you make and run a macro in vim? Here is a simple one to delete lines that have the word ‘test’ in them.
First off, this is all going to happen from right in the editor, while in command mode, not really on the command line, but right from within the editor screen like you would a search. If you are in edit mode like insert or overwrite, simply hit escape.
First off, as you build this macro, you will be actually doing an edit to your file. That is because a macro simply records an action for later replay. So this wouldn’t make sense if you only had one line with the word test in it.
qh <- this starts recording (and will dump the macro in to the h buffer)
/test <- this searches for lines with the word test
dd <- this deletes the line
k <- this moves up a line (in case the next line had the word test in it also)
q <- this stops recording
Now you want to run the macro in the ‘h’ buffer. To do this, type @h. Do you like what happened? OK, now to run the h macro 100 times, simply type 100@h.
Did that delete 100 (if you had that many) lines in your buffer with the word test in it? I hope so.
You can record any vim action in a macro, so have fun with it and see what you can come up with.

