2

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`'
Community
  • 1
  • 1
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171
  • (OT) sorry to disturb: could you reopen http://stackoverflow.com/questions/7568983/vim-usage-as-ide-is-there-a-way-of-sharing-yankbuffers-across-vim-clients-runnin? I had a response that I think was valuable... You can still delete the question if you think that the response doesn't add value on SO. – sehe Sep 27 '11 at 12:48

1 Answers1

4

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.

Andrew Radev
  • 3,982
  • 22
  • 35