Tuesday, August 25, 2009

ack: powerful command line search

ack is a fantastic tool to do recursive searches in text files, such as in your source code. It is simple to use and amazingly fast. See the ack home page for installation instructions. I am on OS X without Macport and installed ack with:

sudo perl -MCPAN -e 'install App::Ack'
Ignoring certain files

Out of the box, ack will ignore "most of the crap you don't want to search" (quoting from their documentation!), but you might want to set it up to ignore more files. For instance, for every JavaScript file (gaga.js) I have a minimized version of that file (gaga-min.js) automatically generated for me. I am never interested in searching in those files. ack doesn't have an option for you to say which files you want to ignore, but you can specify what files you want it to include: -G. This option takes a Perl regular expression. How to write a regular expression that matches any file except those ending with -min.js? With a negative look-behind regexp:
(?<!-min\.js)$
To have this used by default, add the following two lines to your ~/.ackrc:
-G
(?<!-min\.js)$
Adding file types

ack only search in files that are of a "type" it knows about. In general, files are mapped to types by extension, but in some cases they can be mapped by their content. I ran into a case where ack would find what I look for in certain .xpl files1, but not others. The reason was that ack didn't know about the .xpl extension, so it would ignore those files, except those that started with an XML declaration. For those files, even if it didn't recognize the extension, it would recognize the content and know it is dealing with an XML file.

If you run into a similar situation and suspect this is your problem, run the following command which will list the files ack will search in, starting from the current directory:
ack -f .
After you find which file are ignored, add those to your .ackrc so ack recognizes those files. For instance, the following parameters added to your .ackrc will get ack to recognize .xpl and .xsl files as XML files:
--type-add
xml=.xpl,.xsl

1 .xpl are the pipeline files we use in Orbeon Forms.