Vim 101: Search and Replace on Multiple Files

21 篇文章 4 订阅

 vim 可以同时修改多个文件,之前以为只有sed awk才可以,vim可以添加arg list 然后用argdo作用到arg list里所有的文件

ast 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:

Vim buffer list screenshot

Each file in the argument list has been added to the buffer list. Typing :arg will 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 :argagain will confirm this:

Vim :arg screenshot

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值