Giving ack a bit extra do-what-I-mean

Comments
by Jamie Lentin on Jul 15, 2011
ack (or ack-grep) is a command line search tool similar to grep, a few options can make it even more useful

This is just a wee tip, but several people have asked for it so a page would be useful.

Ack is already been discovered by quite a few people, as the URL says it's better than grep. Mostly because it does the right thing in terms of searching directories (the default is to search recursively from the current directory), and the listing formats are much clearer. If you haven't already, get it via. "aptitude install ack-grep".

Configuring to page usefully by default

If you've used ack even once or twice, you'd have noticed that doing "ack | less" is somewhat disappointing. The colour goes away and you're faced with grep style formatting, rather than the colourful ack output you were hoping for. This can be fixed by this one liner alias, which could be placed in your .bashrc.

 alias ack='ACK_PAGER_COLOR="less -x4SRFX" /usr/bin/ack-grep -a'

This does several things to make using ack more pleasant.

  • Debian/Ubuntu can't call the executable ack, since something else beat it to it.
  • If using ack seriously, at some point you will find out it only searches file extensions it knows about (i.e. not .zcml), and loose several clumps of hairs. -a slows ack down slightly, but lets you keep those hairs.
  • The ACK_PAGER_COLOR environment variable means ack output gets paged, but is still colourful. The result of the less options means you get paged results automatically when results fill your screen, without you having to Ctrl-C and re-run through less or something.

Breaking down the options on less:-

  • -x4 tells less to use 4 character tabs
  • -S tells less to not fold long lines, but let them scroll off to the right.
  • -R tells less to use control characters that will colour terminal output, rather than escaping them (used to be -r, thanks Dan Thomas).
  • -F tells less to quit if there is less than one screen, so you don't have to decide whether to page or not beforehand.
  • -X tells less to ignore an initialization termcap might tell it to do. This is useful for xterms, as they are often set up to send applications like less to a secondary screen, and then switch back afterwards. Without it, -F will cause results to appear on the screen and immediately disappear as less quits.

That's about it. Hope it's useful.

 

blog comments powered by Disqus