Free Trial
Learning

The Silver Searcher, a better UNIX search tool

Jacobo García López de Araujo's profile picture Jacobo García López de Araujo on

I was introduced to ack by Xavi Noria several years ago. I liked the usability of the tool but I was so accustomed to find and grep that I never got around to learning it. This post is the perfect excuse to change that and learn ack.

While doing my initial ack research I discovered that there's been an onslaught of new search command line tools in the last few years—focusing in both usability and speed. If you're interested in knowing more about the rest of the tools I will just name a few: ag, pt, sift, ripgrep, ucg and gitgrep.

I decided to compromise on ag also known as The Silver Searcher, an ack compatible tool that searches faster. It is opensource (Apache license), written in C, and mantained by Geoff Greer.

Benefits of ag

  • Focus on usability and cleanliness with improved output.
  • Already packaged or directly available in most unix platforms: Linux, BSD, MacOS.
  • Short command, just type two letters that are located in the home row on your keyboard.
  • Fast. Though I haven't found any conclusive benchmark which compares all the tools (and everyone claims that their tool is faster). I don't care. Sometimes typing an extra word takes longer that the search itself.
  • Editor integration available for Vim, Emacs, and Sublime Text.
  • It supports many of the same grep command line options, so you don't need to learn new flags.

Some key usability features of ag compared to grep

  • Smart case search; no more grep -i.
  • Recursive search is the default; no more grep -r.
  • No need to specify search path, it searches the current directory by default; no more grep ..
  • Supports regexps by default; no more grep -e.
  • Filtering by filename of file extension: no more grep -r <string> | grep.
  • By default it uses ignores VCS files (.gitignore, .hgignore, .svnignore) to skip searching those paths and search faster.

The smart case search feature requires some detailed explanation:

  • If your search query is an all small caps query, it does a case insensitive search.
  • If your search query is contains any upper caps letter, it searches case sensitive for that

Example:

ag trusty will search for any reference to trusty: Trusty, TRUSTy, trusty, TrusTy and so on ag Trusty will search and output only references to Trusty. ag TRUSTY this is the same as the previous example.

If you wish to perform strict case insensitive (for lowercase) queries, you just need to use the -i flag.

A few useful examples:

Basic search, search for all instances of require in your current repo:

ag require

Filter by file extension or file name (using -G flag):

ag -G .markdown dnsimple

Regular expresion search:

ag tiny[-_]p

Search compressed files using -z flag:

ag -z -G .zip packed

It parses from stdin (as any good UNIX tool should):

ag --help | ag search

Learn more

You can learn more about ag in its official github repository.

Share on Twitter and Facebook

Jacobo García López de Araujo's profile picture

Jacobo García López de Araujo

Devops, infrastructure, urban cyclist, music nerd.

We think domain management should be easy.
That's why we continue building DNSimple.

Try us free for 30 days
4.5 stars

4.3 out of 5 stars.

Based on Trustpilot.com and G2.com reviews.