搜索&在 Vim 中使用快速修复列表替换 [英] Search & replace using quickfix list in Vim

查看:25
本文介绍了搜索&在 Vim 中使用快速修复列表替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直使用 EasyGrep 来替换多个文件中的文本.不幸的是,当项目变大时,它会很慢.似乎非常快的一件事是 fugitive.vim 的 Ggrep,它只搜索我的版本控制文件.所有结果也存储在快速修复列表中.

So far I always used EasyGrep for replacing text in multiple files. Unfortunately it is quite slow when a project gets bigger. One thing that seems to be amazingly fast is Ggrep of fugitive.vim that only search my version controlled files. All results are also stored in the quickfix list.

如何使用 Ggrep 的结果对所有找到的文件进行简单替换?是否可以在 quickfix 列表中的所有文件上使用 %s/foo/bar/cg 或者有什么更好的方法?

How can I use the results of Ggrep for doing a simple replace over all those found files? Is it somehow possible to use %s/foo/bar/cg on all files in the quickfix list or are there any better ways?

推荐答案

更新:Vim 现在有 cdo,参见 Sid 的回答.

Update: Vim now has cdo, see Sid's answer.

原答案:

Vim 有 bufdowindo, tabdoargdo,它允许您在参数列表中的所有打开的缓冲区、窗口或文件中执行相同的命令.我们真正需要的是类似quickfixdo 的东西,它会在quickfix 列表中的每个文件上调用一个命令.遗憾的是,Vim 缺少该功能,但是 这是 Al 的解决方案,它提供了一个家庭解决方案.使用它,可以运行:

Vim has bufdo, windo, tabdo and argdo, which let you perform the same command in all open buffers, windows or files in the argument list. What we really need is something like quickfixdo, which would invoke a command on every file in the quickfix list. Sadly, that functionality is lacking from Vim, but here's a solution by Al that provides a home-rolled solution. Using this, it would be possible to run:

:QFDo %s/foo/bar/gc

这将对 quickfix 列表中的所有文件运行 foo/bar 替换.

And that would run the foo/bar substitution on all files in the quickfix list.

bufdowindotabdoargdo 命令有一些共同的行为.例如,如果当前文件不能被放弃,那么所有这些命令都会失败.我不确定上面引用的 QFDo 命令是否遵循相同的约定.

The bufdo, windo, tabdo and argdo commands have some common behaviour. For example, if the current file can't be abandoned, then all of these commands will fail. I'm not sure if the QFDo command referenced above follows the same conventions.

我已经调整了Al 的解决方案 创建一个名为 Qargs 的命令.运行此命令会使用 quickfix 列表中列出的所有文件填充参数列表:

I've adapted Al's solution to create a command called Qargs. Running this command populates the argument list with all of the files listed in the quickfix list:

command! -nargs=0 -bar Qargs execute 'args ' . QuickfixFilenames()
function! QuickfixFilenames()
  " Building a hash ensures we get each buffer only once
  let buffer_numbers = {}
  for quickfix_item in getqflist()
    let buffer_numbers[quickfix_item['bufnr']] = bufname(quickfix_item['bufnr'])
  endfor
  return join(values(buffer_numbers))
endfunction

使用它,您可以按照以下步骤进行项目范围的搜索和替换:

Using this, you could follow these steps to do a project-wide search and replace:

:Ggrep findme
:Qargs
:argdo %s/findme/replacement/gc
:argdo update

(给彼得·林克一个帽子提示)

或者您可以将最后 3 个命令合并在一行中:

Or you could join the last 3 commands together in a single line:

:Ggrep findme
:Qargs | argdo %s/findme/replacement/gc | update

这篇关于搜索&在 Vim 中使用快速修复列表替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆