Useful *nix tools: ack-grep

ack (on Ubuntu named ack-grep, so if you’re using Ubuntu then replace ack in the example commands in this article with ack-grep) is a friendlier alternative to grep for most uses. One of the features I really like is that ack distinguishes files by type and that you can tell ack to explicitly look at certain file types, or to exclude certain file types from your searches. File types that ack it doesn’t know about are not searched, you can get a list of all the types that ack knows by typing ack- grep –help-types.

You can define your own types based on file extensions using –type- set=name=.ext1,.ext2 and then tell ack to include or exclude those as well. You can configure the defaults for ack through a preference file (.ackrc) and/or through environmental variables (ACK_OPTIONS, ACK_PAGER, etc.).

Below is a sample .ackrc file that adds haml and sass file types and tells ack to use less in raw mode to page the results (this allows ack’s friendly colored output in less).

# Use less to page results
--pager=less -r
# Add file types
--type-set=haml=.haml
--type-set=sass=.sass,.scss

Here’s a few example commands taking the above .ackrc into account:

# search only haml files
ack --haml 'my test string' 
# search all files that ack knows about except haml files
ack --no-haml 'my test string'

For more information take a look at ack’s help by running ack –help.

Advertisements
Advertisements
%d bloggers like this: