VIM 多个文件中执行查找与替换

1. 多个文件中执行查找与替换

::: alert-info
substitute命令通常只针对当前文件进行操作,如果想在整个工程范围内进行替换,该怎么办呢?尽管此类场景很常见,但是Vim确实没有提供一条专用的命令,用于工程范围内的查找与替换操作。不过可以把几条操作Vim quickfix列表的简单命令组合起来,间接地实现该功能
:::

refactor-project/
  about.txt
  Pragmatic Vim is a hands-on guide to working with Vim.

  credits.txt
  Pragmatic Vim is written by Drew Neil.

  license.txt
  The Pragmatic Bookshelf holds the copyright for this book.

  extra/
    praise.txt
    What people are saying about Pragmatic Vim...

    titles.txt
    Other titles from the Pragmatic Bookshelf...

把“Pragmatic Vim”都改为“Practical Vim”

:::alert-warning
本节描述的工作流程依赖于:cfdo命令,它在Vim 7.4.858版本中首次被引入。如果你仍在使用旧的Vim版本,需要升级后才能使用该命令。
:::

 /Pragmatic\ze Vim
 :vimgrep // **/*.txt
 :cfdo %s//Practical/gc
 :cfdo update

update 为 写入文件.

说明:
第一: 首先找出要替换的匹配字符 ,
第二: 接着使用 :vimgrep在整个工程范围内查找这个模式,并将结果加入quickfix列表中.
第三: 然后就可以使用 :cfdo在quickfix列表的所有文件中运行 :substitute和 :update命令。