Is ack a better grep?
I’ve been playing with ack the past couple of days and, really, it’s pretty cool. It automatically recurses into directories, only searching through files it knows about, and ignores VCS files and directories, like .svn, .cvs, .hg, etc. You can easily tell it your filetypes with –python, –java, etc. on the command line. It’s in Perl, but it’s also fast. Very fast. And, because it’s Perl, it will run on Windows if you have Perl installed there. I live my Windows life in Cygwin, though, so that is nothing special to me.
So searching a Java project becomes “ack –java thing”. This is better than doing something like
“find . -name *.java|grep -v .svn|xargs grep thing”.
If you put no file path to search on, it will try the files types it “knows” about. Unfortunately for me, it doesn’t know about files that end in “.log”. Know biggie — I just put “*.log” as the search path.
One gotcha is that ack searches with regular expressions, which may be good or it may be bad. It’s good because it provides that power instead of adding an “e” or “-e” to the command. Bad because regular expressions by default may bite you when you least expect it. Like when searching for characters like *,?, etc.
I’ve been using it in my normal work this week and it has proven to be useful. It would be hard to replace grep, but this is a great attempt.
Update Andy Lester, ack’s author, emailed me and said:
You can use –type-set log=.log and ack will recognize it. Or, you can add that to your ~/.ackrc or ACK_OPTIONS. As to the regexes, if you don’t want to think about which characters are special metacharacters, just add the -Q switch.
Thanks Andy, for the hints and the script!