Search Files at Ludicrous Speed on Your Macbook Pro
Software engineers often use tools that are a hundred times better than the stock application that consumers use. Searching for information in text files is very common in the software development world. After all, all code files are just plain text files. If you use text files to store information and want to search them fast, you need Silver Searcher for the command line. Silver Searcher uses threads, and therefore it can use your multi-core CPU more efficiently than traditional command-line search tools like grep.
Getting Silver Searcher (ag)
It is straightforward to install Silver Searcher (ag) for your macOS desktop or laptop if you have homebrew already installed. If you don’t have homebrew installed yet, visit the homebrew website.
Assuming you have homebrew installed, you can install Silver Searcher using a single command from the terminal:
Getting started with Silver Searcher
Once Silver Searcher is installed, you can search for information in text files using the ‘ag’ command. For example, to search for the keyword ‘evoknow,’ I can run:
To search for a phrase such as “UC Berkeley,” I can run:
Advanced Usage of Silver Searcher
Here I will show you a few advanced usages of Silver Searcher.
Finding matching line number and filenames
Sometimes you need the name of the file and the line number of the keyword or phrase that you are looking to find. For example:
The following screen shows an example of the above command. Notice that ag shows the files that matched the phrase, and the line number within each file is shown after the filename separated by a colon.
If you want the names of the files that match, you can use the -l option instead.
Using regular expressions
Using regular expressions in search is very common for software developers. For example, to find all the files that have either ‘Europe/London’ or ‘Europe/Athens’ in them, we can run:
Showing context when searching files
Say you want to find files that contain the word ‘London’ and see a few lines before and after to understand the context where this word appears. You can run:
Here is an example output showing four lines before and after the matches.
Siver Searching (ag) is an fantastic command-line tool for every Mac user. It is also available for Linux. Let me know how you like it in the comment section.