We went over creating VIM macros in a previous tip. If you save your
macros to a file, you can have VIM run the macro against a file,
effectively creating a batch mode for executing VIM macros.
Let’s say that, for whatever reason, you want to convert a file to all
lower case. Create a file called lower.vim and put in the following lines:
ggVGu
:wq!
Make sure there are carriage returns at the end of each line. Those
commands will jump to the first line (gg), turn on visual highlighting
mode (V), then jump to the last line (G). The ‘u‘ command at the end of
the first line will translate all highlighted characters to lower case.
Then the next line writes and quits the file.
Now you can apply this script to any file with:
vim -s lower.vim /path/to/file
Maybe you want to translate all your XML files under the current
directory to lower case. In that case, you can combine VIM with ‘find’
to do some quick batch processing:
find . -type f -name \*.xml -exec vim -s lower.vim {} \;
Note that when you use VIM in this way, VIM does not operate in the
background. An instance of VIM is started for each file being
processed, and the commands in the script file are run like a macro. If
your script file makes a lot of complicated changes, this can be really
amusing to watch. I run this on my computer at home instead of having a
fireplace.
Happy VIMming!
- David Roth

Comments