Matthew Orndoff

Matthew Orndoff

Web Developer, Cold Brew afficiando. Just tryna keep it 💯

ack

this is ack country

ack is like grep.

You can use both commands to search for strings within a file or directory.

But I like ack more.

Here’s why:

  1. ack has a simpler, more intuitive syntax.
  1. ack’s output is pretty by default.

  2. ack only searches things that make sense to search.
    ack searches recursively out-of-the-box
    while ignoring binary files, .git, .svn, and other VCS directories by default.
    It also ignores common backup files created by certain editors.

    • PROTIP ☞ In any given directory, run find . | wc -l to see everything
      grep would search, then run ack -f | wc -l to see everything ack would search.
      I know you didn’t run those commands, but trust me, there’s a big difference and
      that’s good.
  1. ack is portable.
    Installing it on Windows, Unix, or Linux isn’t an issue.
    ack is written in Perl, which is its only dependency.

TL;DR I like ack more becuase it does the same thing grep does, only with nicer defaults and a simpler syntax.

Say YES to life… say YES to ack.

installation

If you’re on Ubuntu or Debian, install the ack-grep package.

sudo apt-get update
sudo apt-get install ack-grep

The executable installs as ack-grep, but we can tell our system to just respond to ack.

sudo dpkg-divert --local --divert /usr/bin/ack --rename --add /usr/bin/ack-grep

If you’re on a Mac and have Homebrew installed, the installation is much more straight-forward.

brew install ack

Beyond that, visit the offical install page for installation instructions on your particular OS.

examples

While wrapping strings in quotes isn’t necessary, it’s encouraged
for legibility.

# Find string "foo" in every file in current directory.
ack foo

# Wrapping strings in quotes insures they aren't intepreted as commands.
ack "foo"

# Find "foo" within .html files in current directory.
ack --html "foo"

# Find "utf" in .html and .json files in current directory.
ack --html --json "utf"

# List default filetype flags.
ack --help-types

# RIP (1983-2017)
ack --bar

using regular expressions

ack uses Perl regular expressions. Here’s a cheatsheet.

# Find IDs in .css files in current directory
ack --css -i "#[-_a-z][-_a-z0-9]*(?=[^}]*\{)"

configuration– .ackrc

Configure ack to do your bidding in a more intelligent way.
Here are some suggestions to make life easier.
Throw these into your .ackrc config file.

# Ignore node_modules directory
--ignore-directory=is:node_modules

# Define custom file filters
--type-set=scss:ext:scss
--type-set=markdown:ext:md,markdown

# If output larger than terminal window, pipe output to less
--pager=less -RFX

wrap-up

I’ve replaced using grep entirely in my workflow.
Maybe some cases will arise where I have use grep again,
and that’s fine.
In the meantime I enjoy using this tool.
There are more advanced options to ack, but this should get you
started for most of what you’re probably trying to do.

Have fun!