Ripgrep: How to Limit Output

Updated

Learn how to limit and shorten the output of ripgrep (rg)

Ripgrep: How to Limit Output

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. If a minified file floods your screen with one giant line, hide long lines with -M:

rg -M 100 "pattern"

Any line longer than 100 characters is replaced with a short note, like [Omitted long line with 25 matches]. Add --max-columns-preview if you'd rather see the start of it.

To stop after a set number of matches in each file, use a lowercase -m:

rg -m 1 "pattern"

Just remember that -m works per file, so five matching files still give you five results. If you only want the names of the matching files, use -l:

rg -l "pattern"

To cap the total number of lines across everything, pipe the results into head:

rg "pattern" | head -n 5
undefined

If you only care whether something matches at all, for example in a script, use -q. It prints nothing and just sets the exit code.