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:
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
- Recursive search is the default; no more
- No need to specify search path, it searches the current directory by default; no more
- Supports regexps by default; no more
- 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.
Smart case search
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
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
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
A few useful examples:
Basic search, search for all instances of
require in your current repo:
Filter by file extension or file name (using
ag -G .markdown dnsimple
Regular expresion search:
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
You can learn more about
ag in its official github repository.