魏长东

weichangdong

vim批量操作文件—绝杀技

Last week I discussed how to use the substitute and global commands for search and replace in Vim. Once these commands have been mastered, they can be combined with argument list commands to apply them to multiple files.

The Argument List

When Vim is started, multiple files can be specified in the command line. This forms the argument list. Starting Vim with vim *.js, then using the :lscommand to list the buffers might display something like this:

vim101-arglist-1.png

Each file in the argument list has been added to the buffer list. Typing :argwill display the argument list. However, typing :arg *.rb will now add all files that match *.rb to the argument list, replacing the old argument list. Typing:arg again will confirm this:

vim101-arglist-1.png

Files can be appended to the argument list by using :argadd, and removed with :argdel. These can be shortened to :arga and :argd.

Running Commands on the Argument List

The :argdo command can be used to execute other commands for all files in the argument list. Let’s say I wanted to rename a variable across lots of files in a project, then all I’d have to do is use a :substitute command with :argdo:
:argdo %s/cmd/command/ge
The e flag, which we haven’t seen before, tells Vim to ignore errors that would otherwise be displayed for files with no matches. Each buffer will be modified without saving any changes, so this pattern is usually combined with :updateto write the files:
:argdo %s/cmd/command/ge | update
In cases where all buffers or windows need to be updated, the :bufdo and:windo commands can be used. I sometimes find myself using :windo when I’m editing a source file and the corresponding unit test in split windows.

Further Reading

All of these commands and features have documentation in Vim’s help system:

  • :help arglist
  • :help :arg
  • :help :argdo
  • :help :bufdo
  • :help :windo