Skip to content

Instantly share code, notes, and snippets.

@jbgo
Created November 22, 2011 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbgo/1386299 to your computer and use it in GitHub Desktop.
Save jbgo/1386299 to your computer and use it in GitHub Desktop.
Project-wide search/replace

The basic form is:

ack --type=js -f app/views/ | xargs sed -i '' -e 's/search/replace/'

I use ack to generate list of files on which I want to perform the search/replace, and then sed does the actual work. It helps to run this on a clean working directory so I can use git diff to examine the results and git checkout -- . to undo everything and try again if I screw everything up.

@jbgo
Copy link
Author

jbgo commented Mar 7, 2013

Here is another way I've been doing this lately:

grep -R -l pattern search_dir | xargs sed -i '' -e 's/search/replace/'

@jbgo
Copy link
Author

jbgo commented Jul 31, 2013

Lately I've starting playing around with vim's :bufdo command to run a command on every file. It's a little easier to remember than the xargs/sed combination, and bufdo let's you use vim macros, which are often more intuitive than regular expressions.

vim $(grep -R -l pattern)
# ...now in vim
:bufdo %s/search/replace/ge | update
:q

See more info on bufdo/argdo commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment