Matthew Orndoff

Matthew Orndoff

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

ack > grep

ack is grep for the 21st century. Unlike grep, you can download it and start using it productively immediately without having to read a manual. ack was created for finding strings within the source code of programs.

Here’s why you should use ack over grep as a your daily driver:

install

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

Find string “foo” in every file in current directory.

ack foo   
# or
ack "foo"

PROTIP ☞ The string you pass ack can either be enclosed in quotes, or not! Realistically, I should be wrapping my strings in quotes so they don’t get interpreted as commands, but I’m lazy.

Find string “foo” in .html files in current directory.

ack --html foo

Find string “utf” in .html and .json files in current directory.

ack --html --json utf

“Woah!”, you say to yourself, “I wonder what other kinds of flags I can pass into ack, like –html and –json?”

List default filetype flags.

ack --help-types

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]*(?=[^}]*\{)"

.ackrc

Configure ack to do your bidding in a more intelligent way.
Here are some things that can make your life a little easier that you can throw into you .ackrc config file.

--ignore-directory=is:node_modules
--type-set=scss:ext:scss
--type-set=markdown:ext:md,markdown
--pager=less -RFX

The first one tells ack to ignore the node_modules directory.
The next two define a scss flag and a markdown flag so that ack only searches those filetypes when we pass it that flag.
The last one pipes output into the less pager if the output from ack is larger than our terminal window.

Here’s a fun one.
RIP (1983-2017)

ack --bar

That’s pretty much all you need to know about ack to get up and running. There are more advanced options you can dive into, but as far as how I use it, this pretty much sums up my use cases.
Have fun!