I'm all about Ember.js recently

Find and Replace in All Files

You want to change a string with another one in some directory in your project.

Here is a one liner to do that:

1
for spec_file in $(ag -g _spec.rb spec/); do sed -i.bak 's/john/jane/g' $spec_file; done

A few caveats:

  • The match should be case-insensitive. Unfortunately, s/john/jane/gI does not work under OSX.
  • The -i tells sed to do an in-place replacement. Again, under OSX, however, you must give an extension for the backup files. A git clean -f can get rid of these in one fell swoop.
  • You can use find spec/ -name '_spec.rb' instead of the ag command. But why would you?

(If you know how to do the replacement with awk, or something similar, please tell me in the comments.)

Your text editor might already have a Find & Replace in project functionality. But maybe it does not. And surely, the command line version is most flexible.

Comments