searchvimrecursion

How can I search and replace recursively in a directory in Vim?


I found out about Vim's substitute command...

:%s/replaceme/replacement/gi

And vimgrep...

:vimgrep  /findme/gj  project/**/*.rb

Is there a way to combine them in order to do a replacement across all the files under a directory?


Solution

  • Even though I'm a Vim user, I generally use find and sed for this sort of thing. The regex syntax for sed is similar to Vim's (they're both descendants of ed), though not identical. With GNU sed you can also use -i for in-place edits (normally sed emits the modified version to stdout).

    For example:

    find project -name '*.rb' -type f -exec sed -i -e 's/regex/replacement/g' -- {} +
    

    Piece by piece:

    This is the general solution with GNU sed and find. This can be shortened a bit in special cases. For example, if you know that your name pattern will not match any directories you can leave out -type f. If you know that none of your files start with a - you can leave out --. Here's an answer I posted to another question with more details on passing filenames found with find to other commands.