在 Vim 中重构 [英] Refactoring in Vim
问题描述
当然,您可以在 IDE 上重构这一事实对许多人来说是无价的,我在编码时几乎不这样做,但在编辑其他人的源代码时我可能会尝试这样做.你如何在 Vim 中完成跨多个文件的如此微不足道的任务?
Of course the fact that you can refactor on IDEs is priceless for many, I hardly ever do it when I am coding but I may try to do it when editing some one else's source. How do you accomplish such a trivial task across multiple files in Vim?
我找到了这个插件来重构 Ruby,但是任何"语言呢?
I found this plugin for refactoring Ruby, but how about "any" language?
推荐答案
我同意Vim 不是 IDE"范式.但有时没有 IDE.这是我在这些情况下使用的:
I agree with the 'Vim is not an IDE' paradigm. But there are times when there isn't an IDE. Here's what I use in those situations:
免责声明:自从我写下这个答案以来,语言服务器协议服务器、linter 和修复程序的无处不在也为 Vim(和其他编辑器)带来了一些很棒的重构功能.IMO 与您在专用 IDE 中看到的功能相比,它们还有很长的路要走(我更喜欢 ALE 和 nvim-lspconfig 用于这些功能).有关详细信息,请参阅有关此问题的其他答案!
Disclaimer: The ubiquity of Language Server Protocol servers, linters and fixers since I wrote this answer have also brought some great refactoring capabilities to Vim (and other editors). IMO they are a long way from the equaling the capabilities you would see in a purpose-built IDE (I prefer ALE and nvim-lspconfig for these features). See other answers on this question for more info!
与常规替换有更多关系的重构我通常在我的项目中使用 :greptree 然后录制一个宏来进行重构- :g 和 :s 不用动脑筋.通常它会让我不费吹灰之力就快速修改大量文件.老实说,我使用这种方法比其他任何方法都多.
Refactoring that has more to do with regular replacements I usually use :grep on my project tree and then record a macro to do the refactor - :g and :s are no brainers. Usually it'll let me quickly modify a large number of files with very little effort. Honestly, I use this method more than any other.
根据您的工作流程,内置命令可能会很慢/不方便.如果你使用 git,那么你会想要使用优秀的 Fugitive 插件及其 :Ggrep
命令只搜索检入 git 的文件.我也喜欢 vim-grepper,因为它的速度和搜索无关(支持 ag、sift、以及更多).
Depending on your workflow the built-in commands might be slow/inconvenient. If you use git, then you'll wanna use the excellent Fugitive plugin and its :Ggrep
command to only search files checked into git. I also like the vim-grepper for its speediness and search-agnostic (supports ag, sift, and more).
:cdo 和 :argdo 可以方便地对一组文件执行 vim 命令.
:cdo and :argdo are handy to execute vim commands over a set of files.
当很难通过 :vimgrep
确定需要更改的文件列表时,我会使用命令行 grep/find 命令来更仔细地整理需要重构的文件列表.将列表保存到文本文件并使用 :e
和宏录制混搭来进行我需要进行的更改.
When it's harder to determine the list of files that need changes via :vimgrep
I resort to the command line grep/find commands to more closely curate the list of files that I need to refactor. Save the list to a text file and use :e
and a mashup of macro recordings to make the changes I need to make.
我发现我的宏录制技能越不生疏,我发现 Vim 在重构方面越有用:从寄存器中保存/恢复感觉很舒服,增加/减少寄存器计数器变量,清理/保存宏录制以供以后使用,等
I find that the less rusty I keep my macro recording skills the more useful I find Vim for refactoring: feeling comfortable saving/restoring from registers, incrementing/decrementing register counter variables, cleaning/saving macro recordings to file for later use, etc.
更新
自从为我所描述的方法写了这篇文章以来,更多的视频广播已经发布在 vimcasts.org 上(我鼓励你观看 所有的 Vimcasts!).要进行重构,请观看以下内容:
Since writing this more videocasts for the methods I describe have been published on vimcasts.org (I encourage you to watch ALL the Vimcasts!). For refactoring watch these ones:
Vimgolf 也是一种很好的练习方式.
Vimgolf is also a great way to practice.
这篇关于在 Vim 中重构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!