The Silver Searcher
An attempt to make something better than ack, which itself is better than grep.
Why use Ag?
- It searches through code about 3x-5x faster than Ack.
- It ignores files matched by patterns in your .gitignore and .hgignore.
- If there are files in your source repo that you don't want to search, just add their patterns to a .agignore file. *cough* extern *cough*
- The binary name is 33% shorter than Ack!
How is it so fast?
- Searching for literals (no regex) uses Boyer-Moore-Horspool strstr.
- Files are mmap()ed instead of read into a buffer.
- If you're building with PCRE 8.21 or greater, regex searches use the JIT compiler.
- Ag calls pcre_study() before executing the regex on a jillion files.
- Instead of calling fnmatch() on every pattern in your ignore files, non-regex patterns are loaded into an array and binary searched.
This blog post gives an idea of how I go about improving performance.
Add the PPA. Make sure you have
python-software-properties installed, then:
sudo add-apt-repository ppa:ggreer/ag sudo apt-get update sudo apt-get install the-silver-searcher
If you get the error
-bash: /usr/bin/ag: No such file or directory, congratulations, you are one of the lucky few. Apt managed to install the i386 version of Ag on an x86-64 system. I have no idea why this happens. To fix this issue, you'll have to
apt-get install ia32-libs or
apt-get purge the-silver-searcher and build from source.
brew install the_silver_searcher
For debs, rpms, and static builds, see the downloads page.
Building from source
- Install PCRE development libraries:
- Run the build script (which just runs aclocal, automake, etc):
- Make install:
sudo make install
Current development status
It's quite stable now. Most changes are new features, minor bug fixes, or performance improvements. It's much faster than Ack in my benchmarks.
ack -i SOLR ~/cloudkick/reach 1.83s user 0.41s system 99% cpu 2.242 total ag -i SOLR ~/cloudkick/reach 0.26s user 0.07s system 99% cpu 0.323 total
You can use Ag with my fork of the popular ackmate plugin, which lets you use both Ack and Ag for searching in Textmate. You can also move or delete
"~/Library/Application Support/TextMate/PlugIns/AckMate.tmplugin/Contents/Resources/ackmate_ack" and run
ln -s /usr/local/bin/ag "~/Library/Application Support/TextMate/PlugIns/AckMate.tmplugin/Contents/Resources/ackmate_ack"
A special thanks goes out to Alex Davies. He has given me some excellent recommendations to improve Ag. Many of these things are still on my list:
mmap(). Remember that's
- pthreads. these days it's silly to use only one core
- Take a look at git. Its use of pthreads is a good example to follow.
search_dir()is definitely sub-optimal. It's doing some work twice.
- Symlink loop detection.
- Behave better when matching in files with really long lines.
- Report "match found at position X of line N" if line is > 10k chars.
- Windows support
stat()are much slower on Windows. Use
- Support Visual Studio instead of autotools?