简体   繁体   English

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

[英]Loading files that meet certain criteria into hidden buffers in vim

I'd like to do some code refactoring in vim. 我想在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. 我想从./src ./include和./tests加载所有.cc,.h和.proto文件。 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. 注意: 我知道您可以在shell中将findsed串在一起 ,但是我尽可能在vim中这样做。 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. 我在上面介绍的模式中的/ gc前缀用于确认每个匹配项是否替换,我需要此功能,因为我通常不想替换某些匹配项,在尝试使用时, findsed解决方案过于严格和挑剔-case,就地替换时也很容易破坏文件。

For reference using sed and find: 供参考使用sed并查找:

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`'

You can use :argadd to add the files you need to vim's argument list. 您可以使用:argadd添加vim的参数列表所需的文件。 This will load them as inactive buffers (you can see them afterwards with an :ls . In your case, it might look like this: 这会将它们加载为非活动缓冲区(之后可以使用:ls看到它们。在您的情况下,它可能看起来像这样:

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

And so on, for the include and tests directories. 等等,对于includetests目录。 You might want to make a command for that or experiment with glob patterns to make it a bit simpler. 您可能想要为此命令或尝试使用glob模式以使其更简单。 Afterwards, your command should work, although I'd recommend running it with :argdo instead: 之后,您的命令应该可以运行,尽管我建议改为使用:argdo运行它:

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. 检查:help argadd:help argdo以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM