Switching to ack 2.0 alpha, why and how?

Ack’s awesome, it’s a better alternative to grep and it belongs to a lot of developers’ toolbox. If you aren’t yet aware about the awesomeness of ack, be sure to read 10 reasons to use ack first!

Every developer has their own style of working, thankfully ack’s optimized for programmers and has very sensible defaults. Did you already know that ack won’t search in binary files by default, nor will it search in files or directories other than your actual source code such as .git/ -directories.

It’s fast because it only searches the stuff it makes sense to search.

On top of those defaults, every user can easily specify personal settings in a .ackrc file located in the user’s home folder.

Ack currently uses a whitelist approach for files and a blacklist approach for folders, this however has its disadvantages. E.g when searching for a certain gem in a Rails project one could try:

ack "gem 'rails'"

This would return zero results, gems are located in Gemfile , a file with no extension. Files without an extension aren’t whitelisted and thus won’t be returned in any results.

A solution for this problem would be to add Gemfile and Gemfile.lock to your .ackrc . In my opinion this would involve too much work and would be too error-prone, a blacklist approach with sane defaults would be much better; too bad ack does not allow specifying which file patterns will be ignored.

Fortunately, the creators of Ack are working on a newer version called Ack 2.0. This is still in an early alpha release (released June 2012), but can already serve more or less as a drop-in replacement for ack. The most important change for me is that ack 2.0 will now search for all text identified files using a blacklist and no longer a whitelist. Be sure to read the github’s readme to know what exactly is new.

Installing ack 2.0.alpha is actually quite straight forward (especially if you’re on a mac and using homebrew), it will only take a moment.

$ brew install https://raw.github.com/lenniboy/ack2-compiled/master/ack2.rb $ brew install ack2

Prompt the following only if you already had ack 1.x installed:

$ brew unlink ack $ brew link ack2

Homebrew symlinks the ack2 executable to ack in your /usr/local/bin directory. Tools such as ack.vim will now use ack 2.0!

Finally we can add our blacklist file patterns in our .ackrc -file, to ignore all files with extension .lol simply add the following entry:

--ignore-file=ext:lol

Or ignore to all files in the log directory:

--ignore-directory=is:log

Finally a big thanks to Andy Lester for creating ack and to ack contributor Rob Hoelz for proofreading this post.

(Source: betterthangrep.com)