One of the handiest tools in my programming toolkit is
ack, a Perl script that is great for searching through code (or really any text files). I use it a lot while refactoring, for example when I want to rename a method every time it’s called in a project.
ackwith this command (from http://betterthangrep.com/):
$ curl http://betterthangrep.com/ack-standalone > ~/bin/ack && chmod 0755 ~/bin/ack
~/bin/ack is in your path searching code is as easy as this:
$ ack my_poorly_named_method
This spits out highlighted matches from every file in the present directory.
ack --help lists a lot of additional options to control how it matches, and what it outputs.
(My favorite is
$ ack --thpppt _ /| \'o.O' =(___)= U Bill the Cat!
So back to how
ack is useful when you want to do a find and replace on a lot of files.
$ ack destroy_other app/models/wd/core/proxy_to.rb 23: def destroy_other! script/pull_wds 58: wd.destroy_other! spec/models/wd/core/proxy_to_spec.rb 216: @wd.destroy_other! spec/script/pull_wds_spec.rb 26: wd.should_receive :destroy_other! 36: wd.should_not_receive :destroy_other!
This next command will open all the files in
vi that matched in the previous command. (The
!! is one of My Favorite Bash Substitution Tricks.)
$ vi `!! -l`
-l option causes it to output only the filenames containing the matches.