As a programmer you often search for a specific strings or regexps in multiple files. Previously i used to do this with the well-known GNU-tools find and grep.

The following command searches for main in all *.go files in the current directory and:

1
2
find . -iname '*.go' | xargs grep -inH  "main"
# -i ignore case, -n print line number, -H print filename

Recently i discovered ag the silver searcher. ag is much faster than grep and searches directories recursively by default. ag respects out-of-the-box your .gitignore files.

You can install ag on a Mac with brew, brew install the_silver_searcher. The following command searches all files in your current directory for the string “main”.

1
ag main

ag_silver_searcher

ag considers your .gitignore automatically but you can tweak it even more with your own .agignore file. I put my .agignore file in my $HOME folder so ag can find it everywhere.

1
2
3
4
5
6
7
# filename: ~/.agignore

node_modules/
build/
target/
your-custom-folder-to-ignore/
*.bak

And if you miss your regular grep-output format, just use:

ag main --vimgrep