Ack - better than grep, a power search tool for programmers

  • Worth mentioning that there is another program in the Ubuntu repos called ack. They've renamed this to ack-grep (don't use Debian so I don't know if the naming convention was pulled in from Debian Unstable or added by the Ubuntu devs).

  • If you are interested, I've posted an interactive shell for ack (and grep) here:

    http://github.com/aristus/ack-shell

        $ acksh ack
    
        1) ./ack-shell/ack-shell
          2:   ## iack (interactive ack)
         15:   prompt = 'ack> '
         44:   cmdline[0] = 'ack'
     
        ack> open 1 44

  • I've been using Ack for a while, and really like it. It saves a few keystrokes, and gives you a nice (colored) output of the results with line numbers as well:

    > grep -r unicorn .

    ./config/unicorn/unicorn_build.rb: logger unicorn_logger

    ./config/unicorn/unicorn_build.rb: old_pid = '/home/rails/xxxxx/current/tmp/pids/unicorn.pid.oldbin

    ./config/unicorn/unicorn_production.rb: '/home/rails/xxxxx/current/tmp/pids/unicorn.pid.oldbin'

    > ack unicorn

    config/unicorn/unicorn_build.rb

    10:# logger unicorn_logger

    14:# old_pid = '/home/rails/xxxxx/current/tmp/pids/unicorn.pid.oldbin'

    config/unicorn/unicorn_production.rb

    15:# '/home/rails/xxxxx/current/tmp/pids/unicorn.pid.oldbin'

  • A nice alternative to my decidedly low-tech approach:

      (in ~/.zshrc)
      # 'ff' command -- find string in files with a specified suffix
      ff() {
        [[ -n $1 && -n $2 ]] || {
          echo "Usage $0 [suffix] [string]"
          echo "Where"
          echo "  suffix  file suffix (without wildcards)"
          echo "  string  string to search for (quote if spaces)"
          return
        }
        find . -name \*$1 -exec grep -Hn $2 "{}" \; | less -RFX
      }
    
    Has colorized output, and only pages (with less) when there is more than one page to show.

  • The time I sometimes lose reading HN pays off when I find stuff like this. BTW the type heuristics is good enough to look inside of files to determine the type: doing 'ack --python pattern' WILL DO look into python files that have no extension (like the toplevel module for instance).

  • I just use 'git grep' to only grep in tracked files. It's worthwhile to learn that tool, for example to learn how you can grep in older revisions.

  • How does one add a custom type to ack and get it to stick? Per the man page, I tried:

         ack --type-set objj=.j
    
    Doesn't work. I can append that to a query:

         ack CPWindowController --type-set objj=".j"
    
    And it will search .j files for CPWindowController, but then it immediately forgets .j and the objj type.

  • I have a wrapper around find that does things like filter out .svn directories. I sometimes use ack for simple searches, but find/xargs/grep together are faster and more explicit in my experience.

  • The Ack Textmate bundle on the site is pretty awesome as well.

  • Is it significantly better than M-x rgrep in Emacs?

  • Being able to use --output with your matched subexpressions is worth going through the trouble of installing it.

  • Love it and have used it for years, A++ would buy again!!