Ripgrep: How to Exclude a Directory

Updated

Learn how to exclude a directory from your ripgrep (rg) search

Ripgrep: How to Exclude a Directory

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 your project folder. To exclude a directory, use the -g flag with an ! in front of the folder name:

rg -g '!dir_name/' "pattern"

Just replace dir_name with your folder, for example rg -g '!tests/' "TODO". The ! means exclude, and the trailing / makes it match folders only, not files with the same name.

Use single quotes around the glob. With double quotes, zsh reads the ! as a history command and you'll get zsh: event not found: tests/.

To skip several folders at once, put them in curly braces:

rg -g '!{dist,build}/' "pattern"

If you only want to skip the folder at the top of your project, and keep folders with the same name further down, add a leading slash: rg -g '!/tests/' "pattern".

undefined

For folders you always want to skip, list them in a .rgignore file in your project root, using the same syntax as .gitignore.