ripgrep: grep, But It Drank Three Espressos and Read Your .gitignore

grep is a legend. It's also happy to search all 80,000 files in node_modules while you wait. ripgrep searches only what matters, very quickly, with sensible defaults. The flags worth knowing, and when old grep still wins.

Tux Harlow

4 min read

You type grep -r "TODO" . in your project. You wait. You wait some more. You hear a faint whirring. grep is now on file 47,000 of node_modules, reading the README of a package that pads strings on the left, and it's thrilled to be there.

[Cut to grep as a tiny librarian, deep in an infinite library, cheerfully reading every book on every shelf. "Ooh, left-pad, version 1.0.3! And 1.0.4! And here's 1.0.3 again in a different folder!" Far, far away, you're still waiting for the word "TODO".]

Meet ripgrep. It's grep's speedy cousin who drank three espressos, read your .gitignore, and skips the parts of the library nobody asked about.

Install it

Bash
sudo apt install ripgrep    # Debian, Ubuntu
sudo dnf install ripgrep    # Fedora

The command is rg. Two letters. Efficient, like everything else about it.

Bash
rg TODO

That's it. No -r, no ., no flags. ripgrep searches recursively from where you are by default, and out of the box it:

  • skips everything in .gitignore, so node_modules, target/, build folders and logs are gone,
  • skips hidden files and folders, so no .git/ internals,
  • skips binary files, so you don't get your terminal screaming random symbols,
  • shows file names, line numbers and coloured matches, grouped by file.

And it's fast. It searches files in parallel and uses a very quick regex engine. Your editor might already be using it: VS Code's search box runs ripgrep under the hood. You've been using ripgrep for years. Surprise!

The flags you'll use every week

Bash
rg -i "connection refused"     # ignore case
rg -w id                       # whole word only (not "valid", "idle"...)
rg -F "array[0].value"         # fixed string, so the brackets and dot aren't regex
rg -l TODO                     # only list file names
rg -c TODO                     # count matches per file
rg -C 3 "panic"                # 3 lines of context either side

-F saves you from regex escaping nightmares. Searching for $user.name() without it is a puzzle. With it, it's just text.

Filter by type and path

Bash
rg -t py "import requests"     # only Python files
rg -T js -F "fetch("           # everything except JavaScript, fixed string
rg -g '*.conf' listen          # only files matching a glob
rg -g '!*.min.js' "function"   # exclude a glob
rg --type-list                 # every built-in type it knows

-t knows hundreds of file types by name: rust, py, go, yaml, md, sh, and more. It's tidier than writing out *.py patterns.

When it hides too much

Sometimes the thing you're looking for is in an ignored or hidden file. Like a .env. Or that log in target/.

Bash
rg --hidden "API_URL"          # include hidden files
rg --no-ignore "error"         # ignore the .gitignore rules
rg -uuu "needle"               # unrestricted: hidden, ignored and binary

Each -u turns off one layer of filtering. -uuu is "search absolutely everything, like grep would". Use it when ripgrep's good manners are getting in the way.

Find and replace? Preview only

Bash
rg "old_name" -r "new_name"

-r (--replace) shows what the lines would look like with the replacement. It doesn't change any files. It's a preview, and a great way to check a rename before doing it for real with your editor or sed. Many a bad sed -i could have been saved by a quick preview.

When plain grep still wins

  • On servers and in scripts. grep is everywhere, ripgrep usually isn't. A script that depends on rg breaks on the first machine without it.
  • In pipes. journalctl | grep error is perfectly good. ripgrep works there too, but there's no reason to install it just for that.
  • When you want to search ignored files by default. That's grep's normal behaviour. ripgrep needs -uuu.

Use rg at your own keyboard, and keep grep in your muscle memory for everywhere else. Our "find: the man page nobody finished" post made the same argument about fd.

Cheat sheet

CommandDoes
rg wordRecursive search, .gitignore respected
rg -i / -w / -FIgnore case / whole word / fixed string
rg -t pyOnly Python files
rg -g '*.conf'Only matching paths
rg -l / -cFile names only / counts
rg -C 3Context lines
rg --hidden, -uuuSearch what it normally skips
rg x -r yPreview a replacement (no changes)

grep is still a legend, and the tiny librarian can keep the job on servers. But on your own machine? Give the espresso cousin a try.