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:
qqqto emptyqregister. If you won’t the previous macro fromqregister will run on step 5.qqto start recording the macro.- Do something to file. For example, remove first line with
dd. - Hummer out
:w | nto write the file and to move to the next one. - Hit
@qto 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 intoqregister and run in this place on the next run. qto stop writing macro.@qto run the macro. It will run untill@qcommand and then run itself.
Macro runs itself untill it bumps into a last file. Notification will appear. Done!