Ripgrep: How to Search for a String in Files

Updated

Learn how to search for a string in files with ripgrep (rg)

Ripgrep: How to Search for a String in Files

Before we start, make sure you have ripgrep installed on your computer. On a Mac, you can install it with Homebrew:

brew install ripgrep

First, open your terminal and navigate to the folder you want to search. Then run:

rg "search_term"

ripgrep searches the current folder and every subfolder, and skips hidden files and anything in your .gitignore, like node_modules. Each result shows the file name, the line number and the matching line, and it comes back almost instantly.

To search only one folder or file, put its path after the text, like rg "TODO" src/.

Keep in mind that ripgrep treats your text as a regular expression. If it contains characters like [, . or *, you might get a regex parse error. Add -F to search for the exact text:

rg -F "price[0]"

If you only want the names of the matching files, add -l.

undefined

To search only certain files, use a glob like -g '*.md' or a file type like -t py.