14 comments

[ 2.2 ms ] story [ 48.3 ms ] thread
I wish ag were on linux boxes by default. Every time I instinctively try to search via ag and am denied I wince as I type in grep instead. :(
I use this to search only php files

ag --stats -G ".*\.php" SearchForThis

How does this differ from ack-grep?
It seems to be a bit faster than ack (which is just called 'ack', Debian the package is called 'ack-grep' to avoid conflicts).
With Ack, I had to set up a bunch of --type-add lines to ~/.ackrc so that Ack wouldn't ignore e.g. SCSS files. ag searches every file by default, except for VCS files (e.g. .git/ and .hg) and files that are ignored by your VCS.

Ag's default behavior is way better, and less confusing to new users. Plus, it's faster.

It's faster than ack (according to their GitHub page), and it honors the .gitignore/.hgignore by default, which is a nice feature!
I've been using it for the past week (on my mac, homebrew is such a joy to use most of the time) and I absolutely love it. Before this I often had to type grep -lir "somestring" *, to recursively search in a project. Which would be often slow (reading huge binary files, ...) and is a handful to type. Admittedly I could've made an alias, but then I still would've had the binary file problem.

Now I can do

   ag "somestring"
And out comes beautifully colorized output in the blink of an eye telling me where and what. In short it comes with sane defaults for code searching and is faster than grep, what's not to like?

I fruitlessly tried ack once before and it never worked out for me, but this is staying in my toolbox.

The silver searcher, would recommend.

I use both ag and ack interchangeably and one thing I wish is that ag would be 100% compatible with ack options. It'd be nice to just do a s/ack/ag and expect things to work.
How does "The Silver Searcher" translate to "ag"?
The chemical symbol for silver is Ag. It's a stretch, but it's short so I'm for it.
The Silver Searcher is very good for using the terminal as an IDE.

I have two aliases which I find very helpful.

- Search Obj-C files:

  alias ago='ag -G ".*\.(h|m)"'
- Search Ruby files:

  alias agr='ag -G ".*.rb"'
Also if you are looking for an alternative to sed don't miss replace[1].

[1] https://github.com/harthur/replace

Not that I have anything against The Silver Searcher, but I've been using the following script I wrote for years, and it is even faster than ag from my very recent limited experience:

    #!/bin/sh -u

    dir="$1"
    shift

    # This doesn't work unless we have GNU find (for the -xtype option):
    gfind "$dir" \
	 \( -type f -or -xtype f \) \
	 \( -name "*.[ct]xx" \
	    -or -name "*.[CchH]" \
	    -or -name "*.py" \
	    -or -name "*.java" \
	    -or -name "*.scala" \
	    -or -name "*.pde" \
	    -or -name "*.js" \
	    -or -name "*.xml" \
	 \) \
	 -print0 \
      | xargs -0 egrep ${1+"$@"}
I've been using ag for the past year or so, and it's great. One pain point remains the lack of Windows support, so I'm back to double-slow cygwin grep when on Windows.