Wednesday 11 January 2012

Excellent tool for code search in local repositories

ack is an excellent tool for search through large local repositories.  It is a Perl based search tool and is amazingly fast.  It has a pre-defined set of file types that it searches in.  You can also define your own file types and store it in your $HOME/.ackrc file.  You can search in all files using the -a option, or search only in some particular file types, by using --type or just --<type>.

For example,

ack --php MyClass

searches for MyClass in all php files.

By default the type php includes only .php files.  You may want to include even .inc files while searching in php.  In that case, you can add --type-add=php=.inc to your $HOME/.ackrc file.

ack is much faster than grep and is much more helpful.  It ignores .svn, CVS and other Version Control System directories by default.

Hence, instead of writing:

grep MyClass $(find . -name "*.php" | grep -v .svn)

you would write:

ack --php MyClass

For details on how to install ack, see http://betterthangrep.com/.  You can read more about ack at http://www.perlmonks.org/?node_id=586862.  The CPAN doc is at http://search.cpan.org/~petdance/ack/ack.