Blog

ack: grep for programmers

Peter McCurdy, Monday, October 26th, 2009

If you find yourself using grep a lot to search through code files, you should probably take a look at ack (packaged as ack-grep in Debian and Ubuntu). It’s like grep, but optimized for searching through codebases, particularly ones that involve multiple languages.

As an example, say you have a project that involves a mix of C++ and Python files, and you want to find all the occurrences of “foo” in just the Python files (this happens to me all the time these days). With grep, you have to say find . -name \*.py | xargs grep "foo", or thereabouts, whereas with ack it’s just ack "foo" --python. For languages with multiple possible extensions (e.g. Perl, C++), this is even nicer.

OK, maybe you’ve already set up all the grep aliases you could ever want, so never type those ugly command lines any more. Even so, I still love ack, because the search results are that much more readable: the filename only shows up once per file, with each match within the file having only the line number taking up space, like so:

$ ack Hello
hello.c
3:// Prints "Hello, world!"
6:    printf("Hello, world!");

I find this makes the results significantly more readable. And what I haven’t tried to replicate above is how it highlights both matches and the filename with a bit of colour, making it even easier to scan.

And on top of all that, “ack” ignores your editor’s swap files by default, and is 25% less typing than “grep”! So give it a spin, I started liking it right away.

4 Responses to “ack: grep for programmers”

  1. Joe Mason commented:

    Does it automatically skip .git and .svn dirs?

  2. pmccurdy commented:

    Yes, as well as CVS, .hg, .bzr, and a bunch of other version control dirs. That said, I never found .git directories were getting in my way much even before I switched to ack, though if I were still using Subversion, that feature alone would have been killer.

  3. mvc commented:

    I was about to say it couldn’t possibly be as cool as pcregrep, favoured tool of perl nerds everywhere and those who have learned to write regex’s like them, but I see it’s just a perl script. Looks good — as you say, not having to pipe everything to “grep -v svn” alone makes it worth using.

  4. John Yerhot commented:

    Ack is great and FAST. FOSSCasts has a screencast going over ack basics too. http://fosscasts.com/screencasts/15-Power-Searching-with-Ack

Leave a Reply