Top 10 reasons to use ack

petdance on 2006-11-30T04:53:40

Yesterday I posted about a snazzy trick with ack, but people pinged me asking what ack is and why they should use it.

ack is the replacement I wrote for grep, aimed at large trees of heterogeneous code.

Using it will change your life, but why? Here's my top 10 list:

  1. Searches recursively through directories by default, while ignoring .svn, CVS and other VCS directories.
    • Which would you rather type?
      $ grep pattern $(find . | grep -v .svn)
      $ ack pattern
  2. ack ignores most of the crap you don't want to search
    • VCS directories
    • blib, the Perl build directory
    • backup files like foo~
    • binary files
  3. Lets you specify file types to search, as in --perl or --nohtml.
    • Which would you rather type?
      $ grep pattern $(find . -name '*.pl' -or -name '*.pm' -or -name '*.pod' | grep -v .svn)
      $ ack --perl pattern
    Note that ack's --perl also checks the shebang lines of files without suffixes, which the find command will not.
  4. File-filtering capabilities usable without searching with ack -f. Want a list of all Perl files in a tree? Use ack -f --perl.
  5. Color highlighting of search results.
  6. Uses real Perl regular expressions, not a GNU subset.
  7. Allows you to specify output using Perl's special variables
    • Example: ack '(Mr|Mr?s). (Smith|Jones)' --output='$&'
  8. Many command-line switches are the same as in GNU grep:
    -w does word-only searching
    -c shows counts per file of matches
    -l gives the filename instead of matching lines
    etc.
  9. ack is pure Perl, so consistent across all platforms.
  10. Command name is 25% shorter. :-) Heck, it's 50% shorter compared to grep -r.

To install it, install the Perl module App::Ack. Your coding life will never be the same.

Visit the home page at


Backup files

uwevoelker on 2006-11-30T06:22:00

ack does include *~ backup files from (x)emacs. Can you please skip them too.
Thanks.

Re:Backup files

petdance on 2006-11-30T06:33:35

Send a note to the bug queue at bug-ack -at- rt.cpan.org

Question

Ovid on 2006-11-30T07:32:53

This isn't a bug report so much as a question: why does that ack info page list all of the switches but ack's documentation doesn't?

Re:Question

petdance on 2006-11-30T15:24:14

Tuit deficit. If anyone wants to help on that, I'd love it.

Zsh!

Dom2 on 2006-11-30T07:48:50

Sounds like you want zsh... Most of the file selection stuff can be done with zsh's extended globbing. Then it becomes available for every application, not just ack/grep.

But the colour highlighting does look cool. :-)

-Dom

Re:Zsh!

ferreira on 2006-11-30T11:26:08

Don't be so mean: zsh isn't for everyone out there. Remember the OS-impaired people. I know they (me among them) possibly can install a port of zsh, but installing ack is so much easier. And there is the portability issue Andy mentioned.

Disclaimer: installing ack is easy if you have a working setting for CPAN, which is not that hard.

Re:Zsh!

Dom2 on 2006-11-30T13:10:39

Well, there's always cygwin if you want bash or zsh under windows. It's not for everybody, but I find it to be a tolerable working environment when I'm in Windows.

But I more just wanted to point out a feature that I use everyday. grep Foo **/*.pm is really handy and I think more people should use zsh and take advantage of these features!

-Dom

God-send utility

ferreira on 2006-11-30T11:31:38

With a little exaggeration, I appreciate ack so much that I have adopted it with the same status of Unix utilities like grep, find, wc, etc. ack is brutally useful in daily development tasks.

The title of the comment is reminiscent to Andy's comment on corelist (which is much more Perl specific).

OK, but first any help with grep --exclude?

mr_bean on 2006-12-01T08:46:03

I'm interested, because using :grep in vim fills up my buffer list with blib/lib files.

If I could get :grep --exclude to work, I might be less interested.

What's the correct way to use grep --exclude to exclude blib/lib files? I can never get it to work.

Re:OK, but first any help with grep --exclude?

arc on 2006-12-01T13:33:14

The current release of GNU grep can't do it.

The CVS tree has support for a new option --exclude-dir, which will allow you to put things like these into your $GREP_OPTIONS environment variable:

  • --exclude-dir=blib
  • --exclude-dir=.svn
  • --exclude-dir=CVS

Though, that said, it's not clear to me how soon there'll be a release of GNU grep supporting that feature.

Re:OK, but first any help with grep --exclude?

petdance on 2006-12-01T13:54:30

Don't bother with the grep. Get ack to take care of it. It ignores .svn and blib, and you can use --perl to only find Perl files.

I'm a happy camper

mr_bean on 2006-12-01T15:54:59

With :se grepprg=ack, I can use ack in vim, and avoid all the buffer list pollution with blib/lib files and backup files I had using grep.