ag is more powerful than ack, or grep. See for yourself.

The original post firstly appeared on my coderwall profile on Jun 14. I don’t feel that coderwall protips are as easily accessible as it used to be so I decided to ‘reprint’ the post here.

I also added some thoughts after using ag in Vim after 6 months.

ag has some advantages over ack, or grep.

  • Faster
  • Excludes files from VCS ignore files (e.g..gitignore)
  • Searches compressed files

You can learn some basics in CONQUERING THE COMMAND LINE book (free) by Mark Bates. I found it yesterday and it’s really good!

In 2.8 Performance Mark compared ag and ack against older Rails code. I did a similar benchmark, let me show you my output:

time ack DHH

real	0m3.040s
user	0m1.561s
sys	0m0.610s

time ag DHH
real	0m0.101s
user	0m0.081s
sys	0m0.137s

Tested on MacBook Air (11-inch, Early 2014)

Going Beyond grep for Searching Source Code is also a good read.

ag in Vim in retrospect (after 6 months)

After 6 months I see ag as a powerful tool to get my job done.

Moving around projects & dependencies

If you’re IDE doesn’t allow you to find files fast, you’re limiting yourself. When I was using RubyMine I often had a feeling that the tool didn’t allow for fast and reliable searching. I always ended up trying to switch between RubyMine fuzzy searcher and the tree view.

ag is different. It shares the UNIX approach that tool should serve one purpose, or adhere to single responsibility principle. It’s always available at your fingertips with :Ag and it’s fast!.

ag is also a powerful tool for searching within 3rd party dependencies included in node_modules or bower_components. Instead of clicking through the directories in some tree view, just start searching like this:

:Ag 'keyForModel' -G .js$ node_modules/ember-cli-mirage/

and adjust the params to scope down the ouput.

Re-indexing when I need it without eating out memory

You have more control over the process as often you don’t need to re-index all files after checking out to another git branch. As a bonus you won’t see the funny situation when all you see for a couple of seconds is a spinner telling you: ‘hey bro! NOW I HAVE TO RE-INDEX ALL THE FILES!’.

Search & replace on the project level

Suppose that I want to change getProject() to getAwesomeProject(). My workflow would be:

# finding all occurences of `getProject`
:Ag getProject -G .js$ app/
:Qargs | argdo %s/getProject/getAwesomeProject/gc | update

Don’t worry about Qargs. It works and has been delivered for you here nelstrom/vim-qargs.

I strongly encourage you to give ag a try!.