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
sudo apt install ripgrep # Debian, Ubuntu
sudo dnf install ripgrep # Fedora
The command is rg. Two letters. Efficient, like everything else about it.
Just search
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, sonode_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
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
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/.
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
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
rgbreaks on the first machine without it. - In pipes.
journalctl | grep erroris 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
| Command | Does |
|---|---|
rg word | Recursive search, .gitignore respected |
rg -i / -w / -F | Ignore case / whole word / fixed string |
rg -t py | Only Python files |
rg -g '*.conf' | Only matching paths |
rg -l / -c | File names only / counts |
rg -C 3 | Context lines |
rg --hidden, -uuu | Search what it normally skips |
rg x -r y | Preview 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.
Linux
More like this
Linux
entr and watch: Make Your Terminal Rerun Things So You Don't Have To
Save, switch window, press up, press Enter, squint. Save, switch window, press up... Stop. entr reruns your command whenever a file changes, and watch reruns it on a timer. Two tiny tools, a thousand saved keystrokes.
Linux
Linux Terminal Toys (and the Surprisingly Useful Tools Hiding Among Them)
A talking cow, a train that punishes typos, the Matrix in your terminal, and hidden among them, six genuinely useful tools you'll keep long after the novelty wears off. A toy box for Linux nerds.
Linux
The /proc Filesystem: Your Kernel Keeps a Diary and Leaves It Open
Your kernel writes down everything: every process, every open file, how long it's been awake. It keeps it all in /proc, and it never locks the drawer. A tour of the proc filesystem, including how to rescue a file you already deleted.