SPUG: ack

Michael R. Wolf MichaelRWolf at att.net
Thu Sep 20 08:46:37 PDT 2007

At the meeting on Tuesday, Jerry's casual mention of 'ack' had me amplify
the usefulness of this tool.  As expected, some folks didn't know about it.
I only learned about it recently.

Let me sing it's praises:
 - It's much better than grep!!!!

  Because it' recursive.
  Because it knows to only look at source code.
  Because it avoids version control directories.
  Because it uses Perl regexps.

Here's an example.  Suppose I wanted to replace all shebang options with an
equivalent pragma.  The regexp should look for anything after "perl" on the
shebang line.

ack --perl '^#!.*perl.+'

The output is human readable (i.e. colorized, multi-line) if it comes to the
screen, and (here's a cool feature) columnized if it's *not* to the screen.
That's another way to say that it's output is identical to grep, therefore
useful to emacs users, like 'grep' or 'compile', as it allows next-error to
sequence through errors in one window with the source in the other window.
(I don't know vi well enough to know if it's got a similar extension.)

More examples.  Find all occurrences of a variable.

  ack --perl '$x'

or function

  ack --perl 'print'

or a module

  ack --perl 'use English'

The file-finding behavior of ack can be accessed independently of the
pattern-finding behavior.  For example, to set the execution bit for all the
perl files in the current directory, do this...

   chmod +x $(ack --perl -f)

I discovered ack about 2 days into coding my own version of it.  I'm sure
I'm not the first one.  I hope I'm the last.  Use ack!

Michael Wolf

P.S.  Oh, yeah.  My favorite feature... You can use ack to output ASCII art
for Bill the Cat, who's famous for speaking this commands name.

  ack --thpppt

_   /|
   U    ack --thpppt!

Michael R. Wolf
    All mammals learn by playing!
        MichaelRWolf at att.net

More information about the spug-list mailing list