Today I Learned

A Hashrocket project

App-Wide Search-Replace in Vim

1) In command-line mode, load all desired files into your argument list (arglist)

:args `git grep -l <string>`

where <string> is the string you want to replace.

2) Run a global search-replace across all the files in your arglist

:argdo %s/<string>/<replacement>/gce | update

Note:

  • Search-replace flags:

    • g[lobal]: replace all matches
    • c[onfirm]: ask for confirmation before replacing
    • e[rror]: don’t halt operation at errors
  • update saves each file after the replacement is made.

  • The search-replace will only apply to files checked into your git history. Be sure to commit the files you want searched before attempting this.

Every developer at Hashrocket is a Vim expert. Check out our development environment, Dotmatrix, and if you are in Chicago, come to the monthly Vim Chicago Meetup hosted at our Chicago office.