Text search inside files is one of the task we, as software engineers, do almost every day. Whether you use an IDE with this functionality built in or not, I believe it’s important to know how to be productive doing this operation from the command line, because in terms of flexibility the command line is really hard to beat. In this post I will show you how to win at file searching using a tool called The Silver Searcher.
grep is a command line utility for searching in files. It takes a pattern and returns the lines matching the pattern.
For example, suppose you are in the root of your project directory and you are looking for all files including the word
grep you can simply run:
grep -R 'password' ./
The terminal will display all the lines with a match for the word
grep is a basic UNIX utility, it’s powerful and it has many options that allows for great flexibility, but it has the drawbacks of being quite uncomfortable to use in software projects.
For example, in 99% of the searches we want to ignore the files inside the
.git/ directory, while we can use the
--exclude-dir option, it still requires some keystrokes making us slow and less productive.
greplooks for the provided pattern in backup files and binary files, leading to false positives.
grepruns also quite slow compared to more optimised alternatives.
For all this reasons, you might want to improve your
grep productivity using The Silver Searcher, or Ag, by Geoff Greer. It smarter and faster than
grep, and easier to use in software projects. It’s also optimised to run on large code bases and you will be impressed by its speed.
If you want another excellent alternative to The Silver Searcher you can try Ack, which is mostly compatible with Ag but a little slower (for now).
In this post I will go through some of the basic commands for searching in files with Ag, so you can compare them with your
grep workflow and see if it can be improved.
First of all, you can install Ag on a Mac using Homebrew:
brew install the_silver_searcher
Or if you are on Linux check here the package name for your platform.
password in the project directory is easy:
This excludes the source control directories by default.
ag --js password
This does not filter only by the
js extension, but also for
You can check the list of file types and their match using
ag --ignore-dir node_modules password
By default Ag reads the patterns in your
.gitignore (or other VCS ignore files) and ignores them (this works also for other source control systems). Beware that there is an open bug report about patterns including a double asterisk.
In addition you can create a
.ignore file in your root directory to add more ignore patterns independently from the ones ignored in your
And if you don’t want Ag to ignore your
.gitignore contents, you can pass a flag to include them:
ag password --skip-vcs-ignores
Sometimes you want to run a search only in files having the name matching a pattern. For example, suppose you want to look for
password only in files with name including the word
ag -G database password
If you want to search in the name of your files you can use
find, but also Ag with the
-g flag. To search for the word
database in your filenames you can then run:
ag -g database
To return case insensitive matches:
ag -i password
To print 5 lines before every match and 5 lines after:
ag -A 5 -B 5 password
Sometimes it’s useful to return only the filenames matching a search instead of the contents of those files. You can do that using the
ag -l password
grep, Ag supports contents from the standard input as source for searching. For example, the following commands pipes the user processes to Ag, which in turn displays the active Neovim processes:
ps -a | ag nvim
Searching in files is so common that using an underperformant tool is a real waste of time. Whether you are looking for variable names, functions definitions or simply looking for the active process on your machine, a better tool makes the difference in terms of productivity and time saved.
As demonstrated in the above examples, Ag is definitely an easier and faster replacement for
grep, so don’t wait for another month before improving your workflow. The effort you will make today to learn it will pay for itself sooner than you expect.