使用 vim 在目录中重构类/方法/字符串的有效方法 [英] Efficient way to refactor a class/method/string within a directory using vim

查看:33
本文介绍了使用 vim 在目录中重构类/方法/字符串的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直在使用查找和替换操作手动重构代码

So far, I have been manually refactoring code by using the find-and-replace operation

%s:/stringiwanttoreplace/newstring/g 

在vim中.

但是,如果我在特定目录内的许多文件中有 stringiwanttoreplace,这是一个缓慢而费力的过程.

But this is a slow and laborious process if I have stringiwanttoreplace in many files inside a specific directory.

我当前/典型的缓慢而费力的过程涉及到 grep:-

My current/typical slow and laborious process involves a grep:-

grep -rn "stringiwanttoreplace" .

在我的终端中显示 stringiwanttoreplace 所在的所有位置/文件名;现在我知道哪些文件包含stringiwanttoreplace,我将一个一个打开每个文件,在每个文件中执行查找和替换操作.

in my terminal to reveal all the locations/filenames where stringiwanttoreplace are; and now that I know which files contain stringiwanttoreplace, I will open each file one-by-one to perform the find-and-replace operation in each file.

是否有更高效的工作流程(在 vim 中)来完成这项工作?

Is there a more efficient workflow (in vim) to get this done?

澄清:我更喜欢基于 vim 的解决方案,而不是 bash 脚本/单行.

CLARIFICATION: I would prefer a vim-based solution instead of a bash script/one-liner.

推荐答案

以下是我将使用的完整命令序列:

Here's the full sequence of commands that I would use:

/stringiwanttoreplace
:vimgrep /<c-r>// **
:Qargs
:argdo %s//newstring/g
:argdo update

在第一行,我们搜索目标模式.填充最后一个搜索模式寄存器(:help quote/),这意味着我们不必再次完整输入.

In the first line, we search for the target pattern. That populates the last search pattern register (:help quote/), which means that we won't have to type it out in full again.

:vimgrep 命令在整个项目中搜索指定的模式.输入 <c-r>/ 作为 ctlr+r 后跟 / - 这将插入最后搜索模式注册到命令行.第一个和最后一个 / 符号是搜索字段的分隔符.尾随的 ** 告诉 Vim 查看当前目录下的每个文件和目录.

The :vimgrep command searches the entire project for the specified pattern. Type <c-r>/ as ctlr+r followed by / - this inserts the contents of the last search pattern register onto the command line. The first and last / symbols are delimiters for the search field. The trailing ** tells Vim to look inside every file and directory below the current directory.

此时,quickfix 列表将填充所有匹配文件的搜索匹配项.:Qargs 是一个自定义命令,它使用 quickfix 列表中列出的所有文件填充参数列表.这是实现:

At this point, the quickfix list will be populated with search matches from all matching files. :Qargs is a custom command, which populates the argument list with all of the files listed in the quickfix list. Here's the implementation:

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

将其添加到您的 vimrc 文件中.

Add that to your vimrc file.

运行 :Qargs 后,我们的参数列表现在应该包含所有包含目标字符串的文件.所以我们可以用 :argdo 运行替换命令,来执行每个文件中的命令.我们可以将替换命令的搜索字段留空,它会自动使用最近的搜索模式.如果需要,您可以在运行替换命令时包含 c 标志,然后系统会提示您进行确认.

Having run :Qargs, our argument list should now contain all of the files that include our target string. So we can run the substitution command with :argdo, to execute the command in each file. We can leave the search field of the substitution command blank, and it will automatically use the most recent search pattern. If you want, you could include the c flag when you run the substitution command, then you'll be prompted for confirmation.

最后,:argdo update 命令保存每个被更改的文件.

Finally, the :argdo update command saves each file that was changed.

正如 @Peter Rincker 指出的那样,您应该确保 Vim 的 'hidden' 选项已启用,否则在写入之前尝试切换到另一个缓冲区时会引发错误对活动缓冲区的任何更改.

As @Peter Rincker pointed out, you should ensure that Vim's 'hidden' option is enabled, otherwise it will raise an error when you try to switch to another buffer before writing any changes to the active buffer.

另外,请注意最后 3 个命令可以在单个命令行中执行,用管道字符分隔它们.

Also, note that the last 3 commands can be executed in a single command line, by separating them with a pipe character.

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

:Qargs 命令来自 这个答案(由我提供),其灵感来自 这个答案 来自 DrAl.发布了一个非常类似的解决方案@ib,这表明 Vim 应该真正实现像 :quickfixdo 这样的东西.

The :Qargs command is pinched from this answer (by me), which in turn was inspired by this answer by DrAl. A very similar solution was posted by @ib, which suggests to me that Vim should really implement something like :quickfixdo natively.

这篇关于使用 vim 在目录中重构类/方法/字符串的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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