将满足特定条件的文件加载到 vim 中的隐藏缓冲区中

发布于 2024-11-27 20:10:13 字数 936 浏览 0 评论 0原文

我想在 vim 中进行一些代码重构。我发现以下 gem 可将转换应用于所有缓冲区。

:dobuf %s/match/replace/gc

我的代码布局为根目录,其中包含依赖项目录和构建目录。我想从 ./src ./include 和 ./tests 加载所有 .cc 、 .h 和 .proto 文件。但不是从依赖项和构建目录进入后台/隐藏缓冲区。我想这样做是使用上面的命令进行重构。

如果有人知道执行用例的更简洁的方法,请展示。

注意: 我知道你可以串起来查找sed 从 shell 执行此操作,但是如果可能的话,我更喜欢在 vim 中执行此操作。我上面介绍的模式中的 /gc 前缀起到确认每次匹配的替换的作用,我需要此功能,因为我经常不想替换某些匹配,findsed在尝试我的用例时,解决方案过于严格和挑剔,在进行就地替换时也很容易破坏文件。

作为使用 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 sedsolution 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待圉鍢 2024-12-04 20:10:13

您可以使用 :argadd 将所需的文件添加到 vim 的参数列表中。这会将它们加载为非活动缓冲区(您可以随后使用 :ls 查看它们。在您的情况下,它可能看起来像这样:

argadd src/**/*.cc
argadd src/**/*.h
argadd src/**/*.proto

等等,对于 includetests 目录。您可能想要为此创建一个命令或尝试使用 glob 模式以使其更简单,但您的命令应该可以工作,尽管我建议使用 运行它: argdo 相反:

argdo %s/match/replace/gc

这只会执行它您明确指定的缓冲区,而不是您当时可能打开的任何其他缓冲区,请检查 :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:

argadd src/**/*.cc
argadd src/**/*.h
argadd src/**/*.proto

And so on, for the include and tests 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:

argdo %s/match/replace/gc

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文