Automated editing of multiple files with vim
Sometimes you need to process a heap of files, selecting them by mask or by list. It’s not common deal so I usually forget a sophisticated argdo
syntax and do it manually.
But it turned out to be easy! I use mvim, but it will work to vim and vi as well.
Firstly, we open a bunch of files:
mvim filename-wildcard-1.txt filename-wildcard-2.txt filename-wildcard-3.txt
Or use a wildcard:
mvim filename-wildcard-*.txt
You will see the first file of the bunch. Now we going to make the same changes to every file using the recursive macro:
q
q
q
to emptyq
register. If you won’t the previous macro fromq
register will run on step 5.q
q
to start recording the macro.- Do something to file. For example, remove first line with
d
d
. - Hummer out
:w | n
to write the file and to move to the next one. - Hit
@q
to run current macro recursively. There’s a trick! On step 1. we’ve cleared the q register and nothing will happen. But after 6 step macro will be written intoq
register and run in this place on the next run. q
to stop writing macro.@q
to run the macro. It will run untill@q
command and then run itself.
Macro runs itself untill it bumps into a last file. Notification will appear. Done!