Hi there! I'm Shrijith Venkatrama, founder of Hexmos. Right now, I’m building LiveAPI, a tool that makes generating API docs from your code ridiculously easy.
Hey there, devs! If you’ve ever found yourself drowning in files, desperately searching for that one line of code or text, then buckle up.
Today, we’re diving into ripgrep—a blazing-fast, command-line search tool that’s about to become your new best friend.
Think of it as grep
on steroids, but with a personality that’s way more fun to work with.
Whether you’re just starting out or ready to wield its full power, this guide will take you from “What’s ripgrep?” to “I’m basically a search wizard now.”
Getting Started: Ripgrep Basics for Newbies
So, you’ve installed ripgrep (if not, check the official install guide), and you’re staring at your terminal wondering where to begin.
Don’t worry—I’ve got you. Ripgrep, or rg
for short, is all about searching files for patterns.
It’s simple at its core: give it a pattern, and it’ll hunt through your files line by line, spitting out matches with line numbers.
Let’s try it out.
Imagine you’re poking around the ripgrep source code (grab it from this archive if you want to follow along). You want to find every line in the README.md
that mentions “fast.” Here’s how:
rg fast README.md
Boom! You’ll see something like:
75: faster than both. (N.B. It is not, strictly speaking, a "drop-in" replacement
88: color and full Unicode support. Unlike GNU grep, `ripgrep` stays fast while
119:### Is it really faster than everything else?
124:Summarizing, `ripgrep` is fast because:
129: optimizations to make searching very fast.
What’s happening here? Ripgrep reads README.md
, finds lines with “fast,” and prints them with line numbers. If your terminal supports colors, “fast” will even be highlighted. Pretty neat, right?
Now, let’s spice it up with a regular expression. Say you want lines where “fast” is followed by word characters (like “faster” but not just “fast”). Try this:
rg "fast\w+" README.md
This time, you’ll get:
75: faster than both. (N.B. It is not, strictly speaking, a "drop-in" replacement
119:### Is it really faster than everything else?
Here, \w+
means “one or more word characters,” so it skips standalone “fast” matches. Want to dig deeper into regex? Check out ripgrep’s regex syntax docs—but for now, you’re off to a solid start!
Leveling Up: Searching Folders Like a Pro
Alright, you’ve nailed searching one file. Now let’s scale it up. Ripgrep’s default superpower is recursive search—it’ll dig through your entire directory tree without you lifting a finger. No explicit path? It assumes ./
and gets to work.
Back in our ripgrep source folder, let’s find all function definitions named write
:
rg "fn write\("
You’ll see hits across multiple files:
src/printer.rs
469: fn write(&mut self, buf: &[u8]) {
termcolor/src/lib.rs
227: fn write(&mut self, b: &[u8]) -> io::Result<usize> {
250: fn write(&mut self, b: &[u8]) -> io::Result<usize> {
Why escape the (
? In regex, parentheses have special meaning, so \(
tells ripgrep to treat it literally. Want to avoid regex entirely? Use -F
for fixed strings: rg -F "fn write("
.
What if you only care about the src
folder? Just point ripgrep there:
rg "fn write\(" src
Now it’s scoped to src/printer.rs
. This is where ripgrep shines—effortless recursion with pinpoint control. Oh, and here’s a pro tip: ripgrep respects your .gitignore
by default, so it skips stuff like target/
or node_modules/
. No clutter, just results.
Smart Filtering: Cutting Through the Noise
Here’s where ripgrep starts flexing. It’s not just about finding stuff—it’s about finding the right stuff. By default, it skips hidden files, binary files, and anything in your .gitignore
. But you can tweak that, and things get even cooler with manual filtering.
Automatic Filtering Magic
Let’s say you’re in a Git repo. Ripgrep reads .gitignore
, .ignore
, and .rgignore
files, prioritizing them in that order. Got a log/
folder ignored by Git? Ripgrep won’t touch it—unless you override it with a .rgignore
saying !log/
. Want to see it in action? Use --debug
to peek under the hood.
Need to break free? Flags like --no-ignore
(skip all ignore files) or --hidden
(include dotfiles) give you control. For binary files (think PDFs or images), --text
(-a
) forces a search, but beware—binary data might mess with your terminal.
Manual Filtering with Globs
Now, let’s get surgical. Say you only want Cargo.toml
files to check for “clap” dependencies:
rg clap -g "*.toml"
Output:
Cargo.toml
35:clap = "2.26"
51:clap = "2.26"
The -g "*.toml"
flag filters to TOML files only. Want everything except TOML? Add a !
:
rg clap -g "!*.toml"
File Types: Your Shortcut to Sanity
Typing globs repeatedly gets old fast. Enter file types. Ripgrep has built-in types like rust
(*.rs
) or c
(*.{c,h}
). Search Rust files for “fn run”:
rg "fn run" -trust
Same as -g "*.rs"
, but shorter. List all types with rg --type-list
. Need a custom type? Define it on the fly:
rg --type-add "web:*.{html,css,js}" -tweb "title"
Here’s a quick cheat sheet:
Flag | What It Does | Example |
---|---|---|
-t rust |
Include Rust files | rg -trust "fn" |
-T rust |
Exclude Rust files | rg -Trust "fn" |
--type-add |
Add a custom type | See above |
Filtering is where ripgrep turns from a tool into a lifestyle. You’re not just searching—you’re curating.
Power Moves: Custom Configs and Replacements
You’re no longer a beginner—you’re a ripgrep enthusiast. Time to make it yours and bend its output to your will.
Configuration Files: Set It and Forget It
Tired of typing the same flags? Create a config file. Set RIPGREP_CONFIG_PATH
to, say, ~/.ripgreprc
, and add your defaults:
--max-columns=120
--hidden
--type-add
web:*.{html,css,js}
Now rg
uses these automatically. Override them on the fly with, say, -M0
to disable the column limit. Debug with --debug
if it’s acting funky, or use --no-config
to ignore it entirely.
Replacing Text: Output Hacks
Ripgrep doesn’t edit files, but it can transform what you see. Replace “fast” with “QUICK” in README.md
:
rg fast README.md -r QUICK
Output shifts to:
75: QUICKer than both. (N.B. It is not, strictly speaking, a "drop-in" replacement
88: color and full Unicode support. Unlike GNU grep, `ripgrep` stays QUICK while
Want just the matches? Pair it with --only-matching
(-o
):
rg fast README.md -or QUICK
75:QUICK
88:QUICK
119:QUICK
124:QUICK
129:QUICK
For regex fans, use capturing groups. Join “fast” and the next word with a dash:
rg "fast\s+(\w+)" README.md -r "fast-$1"
88: color and full Unicode support. Unlike GNU grep, `ripgrep` stays fast-while
124:Summarizing, `ripgrep` is fast-because
$1 grabs the first group. Name it with (?P<name>\w+)
for clarity: -r "fast-$name"
. Output stays the same, but your intent shines through.
Master Tier: Preprocessors and Beyond
Welcome to the elite level. You’re not just searching text anymore—you’re searching anything.
Preprocessors: Search the Unsearchable
Got a PDF you want to grep? Ripgrep can’t read it raw, but a preprocessor can. Download this dissertation and try:
rg "The Commentz-Walter algorithm" 1995-watson.pdf
Nothing. PDFs are binary. Enter pdftotext
(from poppler). Create a script preprocess.sh
:
#!/bin/sh
exec pdftotext - -
Then:
rg --pre ./preprocess.sh "The Commentz-Walter algorithm" 1995-watson.pdf
Matches galore:
316:The Commentz-Walter algorithms : : : : : : : : : : : : : : :
7165:4.4 The Commentz-Walter algorithms
How it works: Ripgrep runs the script for each file, passing the file path and piping its contents to stdin. Optimize with --pre-glob "*.pdf"
to skip non-PDFs, slashing overhead.
Encoding and Binary Tricks
Ripgrep assumes ASCII-compatible encodings (UTF-8, latin1) by default, sniffing UTF-16 BOMs for Windows folks. Force an encoding with -E
, or go raw with -E none
for byte-level searches. Disable Unicode in regex with (?-u:.)
to match any byte—handy for binary files.
For binary data, use -a
to search as text, or --binary
to stop at matches without dumping garbage. Consistency bugging you? --no-mmap
standardizes detection.
Wrapping Up
From your first rg fast
to preprocessing PDFs, you’ve gone from novice to power user. Ripgrep’s speed, flexibility, and clever defaults make it a game-changer. Play with it, break it, tweak it—your search game will never be the same. Got questions? Hit up the ripgrep GitHub or drop a comment below. Happy grepping!
Top comments (0)