# Ripgrep's trade-off: better to miss a search than to give you noise

Date: 2026-01-24
Category: Technology
Tags: Open-Source Tools, Code Search, Design Trade-offs
Summary: By default, ripgrep skips gitignore files, rejects backtracking regex, and builds no index—every choice comes at a cost. This piece explains why I agree with these trade-offs and the few habits I changed as a result

A colleague borrowed my terminal, typed `rg TODO`, then turned and asked me: why does your repo only have three TODOs?

I said, add `-u` and take another look. He typed it, and the screen scrolled for two pages.

This isn't a bug, and it's not that he doesn't know how to use it. It's a trade-off ripgrep deliberately made: better to miss some things than to flood you with noise first. I haven't seen this default much in other search tools, and the longer I use it, the more I think it's right.

## The default blocks noise for you, at the cost of making you think it's broken the first time

When ripgrep starts, it reads three kinds of rule files—`.gitignore`, `.ignore`, `.rgignore`—skips hidden files, and stops when it hits a binary. Stack these three layers of filtering, and a normal frontend repo can have over ninety percent of its files skipped.

The cost is real: first-time users often think search is broken. I've seen issues asking why `.env` can't be found; I've tripped over it myself once, hunting for a config file while debugging a build issue, only to realize its name starts with a dot and it was skipped as a hidden file.

I still think this trade-off is right. Search's signal-to-noise ratio doesn't depend on the tool itself; it depends on how many junk files are in the repo, and that quantity is often tens or hundreds of times the business code. Having the tool default to the clean side means "I want to see dirty data" becomes an action you have to explicitly express. The opposite design—give everything by default, then make everyone add their own exclusion rules—means everyone pays the cost every day for the occasional convenience of a few.

The boundary is here too: it reads ignore files in the directory tree. If you pile several cloned repos into a directory with no ignore rules and search there, filtering weakens a lot, and the "can't find it" problem flips into "too many results." So its cleanliness has a precondition: your repo structure is roughly normal.

I also like how it concedes: not a single `--all` switch that turns everything on, but three `u` that stack level by level.

```bash
rg TODO            # 尊重 ignore、跳过隐藏、跳过二进制
rg -u TODO         # 忽略 ignore 规则
rg -uu TODO        # 再加上隐藏文件
rg -uuu TODO       # 再加上二进制
```

Each extra `u` costs a bit more and only buys one more layer. This gradient is more honest than one-click-all-on: it forces me to think about what I'm actually looking for. To find `.env` I only need `-uu`; dragging binaries in too is just asking for trouble.

## Cutting backtracking regex for an upper bound you can put in an SLA

ripgrep's regex engine is Rust's `regex` crate, whose core guarantee is linear time: match time is proportional to input length, independent of pattern complexity.

This guarantee isn't free. It doesn't support backreferences or lookaround; something like `(foo)\1` errors out directly instead of quietly running slow. To use those features you add `-P` to switch to the PCRE2 engine, handing over the worst-case guarantee.

In the code-search scenario, this trade is a good deal. Because the input isn't a carefully constructed string—it's arbitrary text from someone else's repo: compressed JS, base64, tens of thousands of lines of JSON with no newlines, a blob of minified output. On such input a backtracking engine isn't just slower; it pins a core and hangs. I once ran a search with `-P` in CI, happened to hit build artifacts, and the job ran a long time before timing out; I don't remember exactly how long, but since then I've been a bit wary of backtracking regex.

There are times you need `-P`, mainly for parsing logs: using `(?<=...)` to skip a prefix, or `\K`. In those cases I first narrow candidates to a few files, then enable PCRE2—I won't use it to scan a whole repo. The dividing line isn't "is PCRE2 good," it's whether the input is controllable: if the input is a few lines of logs you wrote, backtracking risk is basically negligible; if the input is someone else's repo, don't gamble.

## No index, and no mmap by default

This is the part I appreciate most.

ctags, cscope, and zoekt take another path: build an index first, then query it. Fast, sure, but an index has state—it doesn't know when you change code. So "can't find it" gets a second cause: the first is ignore rules, the second is a stale index. With two causes stacked, when debugging you first have to tell which one it is, and telling them apart itself takes time.

Indexes are genuinely valuable on huge repos: millions of files, repeated full-text searches, team sharing. But those scenarios usually deserve a dedicated search service, not a command-line tool moonlighting. ripgrep stands on the other side, reading from the filesystem fresh each time. Cold start is slower, and a full scan of a big repo can max out the CPU, but in exchange "can't find it" has only one explanation left. When handling production issues I value this a lot: one fewer variable is one fewer variable.

mmap is another interesting trade-off. Many search tools enable it by default, seemingly saving a data copy. ripgrep doesn't by default, roughly because mmap turns disk I/O into page faults, handing control to the kernel; and if a file is truncated by another process during the search, the process gets SIGBUS directly. Also, in repos with many small files, mmap's benefit is negligible anyway, while adding a layer of syscall overhead.

I haven't measured this rigorously; I just accept the author's judgment—he's been far more careful than me with measurements on these low-level details. If you really want to verify, turn on `--mmap` yourself and compare timings; conclusions may differ by repo, and between spinning disks and NVMe.

There's another implicit trade-off: it has no interactive UI. Paging through results, previewing, jumping—those are left to fzf, telescope, or editor plugins; it just outputs matches to stdout and exits. The upside is I almost never worry about formatting when using it in a pipeline; the downside is first-time users find it awkward—it looks crude because it only does half the job, and the other half is elsewhere.

## A few habits it made me change

After using it a while, some actions got replaced.

I no longer list files with `find`; I just use `rg --files -g '*.ts'`. It respects ignore rules too, and what it outputs is "the repo as the tool sees it"—handy for coverage stats or batch scripts, no more hand-writing logic to exclude `node_modules`.

Searching by type is smoother than by glob. `rg -tpy` searches only Python, `rg -tjs` only JS, no need to remember full suffix patterns. To add a custom type, use `--type-add 'conf:*.{yaml,toml,ini}'`.

Add `-F` for literal searches. Searching strings with special characters like `foo(`, `-F` saves a pile of escaping, bypasses the regex engine, and incidentally removes a class of "why didn't this bracket match" confusion.

Add `--sort path` when you need stable output in scripts. By default it searches in parallel, and result order across files is nondeterministic—a concession for throughput. For human eyes it doesn't matter, but for diffing or test snapshots you need sorting, at the cost of losing parallelism.

In CI I use `rg --no-ignore --hidden -c` to scan once, specifically to catch secret files that are hidden by ignore rules but actually committed. This usage is a bit counterintuitive: normally it helps me ignore things, but here I tell it not to ignore. The same mechanism, turned in a different direction, serves another purpose.

## Some jobs I don't let it handle

Once you get used to it, it's easy to get the illusion that it can do everything. I've drawn a few lines for myself:

- Interactions that require an index to work, such as jumping to symbols or looking up global references across hundreds of thousands of files. rg can find strings, but it can't tell whether that's a variable or a sentence in a comment—that kind of job goes to an LSP or an indexing service.
- Batch renaming and replacement. It's read-only and outputs results to stdout. If you actually want to modify files, use sed, an editor, or a codemod—at least those give you a diff to look at before writing to disk.
- Fault tolerance and semantic matching. Misspell one letter and it won't find it. That's the nature of literal matching, not a configuration problem.
- Cross-repository, permission-aware aggregated search. That's a server-side job; a single-machine command-line tool has no advantage there.

Next time `rg` doesn't find something in an unfamiliar repository, don't doubt the tool first. Run `rg --files | wc -l` to see how big the world looks in its eyes, then decide whether to add that `u`.
