ack

grep optimised for programmers
2014-08-25
~
2 min read

The grep utility may well be one of the best tools for programmers ever made. Simple, powerful, with an endless list of options. When combined with the find program, it makes the ultimate solution for searching in your codebase. It's almost unbelievable that Ken Thompson first wrote it for Unix Version 4 in 1973. But that is also the problem.

Codebases looked very different 40 years ago. Thanks to the build and version control systems, and IDE's that are available today, projects grow much larger and get more complex. We have to deal with much more information at the same time, and while it is possible to set up grep to match our current requirements, it may not always be as elegant. Fortunately, there are alternatives designed for what we need today. One of them is ack.

ack does essentially the same thing as grep, but with several key improvements that are invaluable when used to search through code. By default, it searches through directory trees rather than stdin, all you need is to specify the regular expression pattern to search for. When traversing your codebase, ack is smart enough to ignore VCS directories, such as .git/ and .svn/, text editor backups, binary files and other rubbish that would otherwise pollute your search results. That, by the way, makes it much faster than grep, because it has to process less data. And on top all of that, it uses terminal colours to highlight matches within the lines and to visually separate lines from different files. Well, that's not bad, is it?

ack output example
An example of the output of the ack command.

The picture above shows what running the ack command does with no additional configuration. You just need to specify the pattern, it's as simple as that.

You can, of course, set up grep to do many of these things too, if not all of them. Especially in combination with the find command. It can search directories recursively, exclude file patterns, and also print matched substrings in colour. Along with find, you'll be able to ignore binaries and other types of files when searching. However, having all these options in place, you'd probably have to set up several aliases on your system. It is the convenience that makes the difference when you need to do this 50 times a day.

Needless to say, you can change the hell out of ack with many command-line options and modifiers, see the docs for the full list. There's no fancy file-format, you just save your preferred options to an .ackrc file, one per line. There are several places to keep these files, a system-wide one /etc/ackrc, per-user config ~/.ackrc, and also per-project one ./.ackrc. So if you would like to use different file filters for different projects, just keep them in the project tree. Or why not commit them to git for others as well?

My ack configuration
My current ack configuration and how it looks.

The ack utility is no new kid on the block. It was made by Andy Lester almost 10 years ago with contributions from many others over the years. It is available for anyone under the Artistic License 2.0 (hosted on Github). If you'd like to make searching through code easier and more efficient, ack is a fantastic tool to try!