将满足特定条件的文件加载到 vim 中的隐藏缓冲区中
我想在 vim 中进行一些代码重构。我发现以下 gem 可将转换应用于所有缓冲区。
:dobuf %s/match/replace/gc
我的代码布局为根目录,其中包含依赖项目录和构建目录。我想从 ./src ./include 和 ./tests 加载所有 .cc 、 .h 和 .proto 文件。但不是从依赖项和构建目录进入后台/隐藏缓冲区。我想这样做是使用上面的命令进行重构。
如果有人知道执行用例的更简洁的方法,请展示。
注意: 我知道你可以串起来查找
和 sed
从 shell 执行此操作,但是如果可能的话,我更喜欢在 vim 中执行此操作。我上面介绍的模式中的 /gc 前缀起到确认每次匹配的替换的作用,我需要此功能,因为我经常不想替换某些匹配,find
和 sed在尝试我的用例时,解决方案过于严格和挑剔,在进行就地替换时也很容易破坏文件。
作为使用 sed 和 find 的参考:
列出候选替换:
find src include tests -name *.h -or -name *.cc -or -name *.proto|
xargs sed -n 's/ListServices/list_services/p'
执行替换:
`find src include tests -name *.h -or -name *.cc -or -name *.proto|
xargs sed -i 's/ListServices/list_services`'
I'd like to do some code refactoring in vim. I have found the following gem to apply transformations to all buffers.
:dobuf %s/match/replace/gc
My code is layed out with the root directory having a directory for the dependencies and a build directory. I want to load all .cc , .h and .proto files from ./src ./include and ./tests. But not from the dependencies and build directories, into background/hidden buffers. I want to do this to do the refactor using the command above.
If someone knows of a cleaner way to perform the use case, please show it.
Note: I know you can string together find
and sed
to do this from the shell, however I prefer doing it in vim , if at all possible. The /gc prefix in the pattern I presented above serves the role of confirming replacements on each match, I need this functionality as often I don't want to replace certain matches, the find
and sed
solution is too restrictive and finicky when attempting my use-case, it is also easy to destroy files when doing in-place replacements.
For reference using sed and find:
List candidate replacements:
find src include tests -name *.h -or -name *.cc -or -name *.proto|
xargs sed -n 's/ListServices/list_services/p'
Perform replacements:
`find src include tests -name *.h -or -name *.cc -or -name *.proto|
xargs sed -i 's/ListServices/list_services`'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
:argadd
将所需的文件添加到 vim 的参数列表中。这会将它们加载为非活动缓冲区(您可以随后使用:ls
查看它们。在您的情况下,它可能看起来像这样:等等,对于
include
和tests
目录。您可能想要为此创建一个命令或尝试使用 glob 模式以使其更简单,但您的命令应该可以工作,尽管我建议使用运行它: argdo
相反:这只会执行它您明确指定的缓冲区,而不是您当时可能打开的任何其他缓冲区,请检查
:help argadd
和:help argdo
以获取更多信息。You can use
:argadd
to add the files you need to vim's argument list. This will load them as inactive buffers (you can see them afterwards with an:ls
. In your case, it might look like this:And so on, for the
include
andtests
directories. You might want to make a command for that or experiment with glob patterns to make it a bit simpler. Afterwards, your command should work, although I'd recommend running it with:argdo
instead:This will only execute it for the buffers you explicitly specified, not for any of the other ones you might have opened at the time. Check
:help argadd
and:help argdo
for more information.