开发者

Loading files that meet certain criteria into hidden buffers in vim

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 fi开发者_Go百科nd 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`'


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜