DEV Community

Cover image for ripgrep: Not Just a Faster grep, but a Sharper One
Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on

5 3 3 3 3

ripgrep: Not Just a Faster grep, but a Sharper One

When you think ripgrep (rg), you probably think "fast regex search tool."

But what makes it seriously lethal is how much you can control its behavior with fine-grained options.

VS Code? It internally uses ripgrep for its file search.

If you tweak ripgrep, you're basically tweaking a search engine level tool.

Let's slice deep into 10 seriously useful ripgrep options and why they matter.

1. -F / --fixed-strings

Treat your search pattern as a literal string, not a regex.

Useful when: You don't want your . or * or () being treated as regex syntax.

Example:

rg -F "user.name" ./src
Enter fullscreen mode Exit fullscreen mode

No regex engine overhead. It’s faster.

2. -g / --glob

Include/exclude files manually.

Useful when: You want to surgically search only .ts files, or exclude node_modules.

Examples:

rg -g "*.ts"
rg -g "!node_modules/**"
Enter fullscreen mode Exit fullscreen mode

Want case-insensitive globbing? Use --glob-case-insensitive.

3. -U / --multiline

Match patterns that span across multiple lines.

Useful when: You're trying to find multiline JSON fields, big HTML tags, broken SQL queries.

Example:

rg -U '"description":\s*"([^"]|\n)*?"' ./configs
Enter fullscreen mode Exit fullscreen mode

When one line isn't enough — -U is your buddy.

4. -P / --pcre2

Use PCRE2 (Perl Compatible Regular Expressions) engine.

Useful when: You need crazy powerful regex features like lookaheads, lookbehinds, atomic groups, etc.

Example:

rg -P "(?<=error_code: )\d+" logs/
Enter fullscreen mode Exit fullscreen mode

Without -P, ripgrep's default engine won't support lookbehinds.

5. -A, -B, -C

Show lines after, before, or around a match.

Useful when: You want surrounding context, not just isolated matching lines.

Examples:

rg -C 3 "panic:" ./backend
rg -A 5 "Traceback" ./logs
Enter fullscreen mode Exit fullscreen mode

Makes your search results feel story-like instead of fragmented.

6. -l / --files-with-matches

Only print filenames that have at least one match.

Useful when: You're building file lists (for further scripting), not inspecting contents.

Example:

rg -l "TODO" ./src | xargs vim
Enter fullscreen mode Exit fullscreen mode

(VS Code does a version of this under the hood when showing "files with results".)

7. -o / --only-matching

Print only the matching text, not the whole line.

Useful when: You want just emails, IDs, tags, etc.

Example:

rg -o -P "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b" ./emails.txt
Enter fullscreen mode Exit fullscreen mode

Think: turning a noisy search into a clean data extraction tool.

8. --json

Output matches in JSON lines format.

Useful when: You want machine-readable results (for piping into scripts, or UIs).

Example:

rg --json "error" ./logs | jq .
Enter fullscreen mode Exit fullscreen mode

VS Code search runs a variation of this to parse results live.

9. --type / --type-not / --type-add

Search specific file types or define your own.

Useful when: Built-in file type matching isn't enough.

Examples:

rg --type js "use strict"
rg --type-not css "container"
rg --type-add 'proto:*.proto' --type proto "service"
Enter fullscreen mode Exit fullscreen mode

You can extend ripgrep's world. Define your own "languages".

10. -r / --replace

Replace matched text directly, like sed, but faster.

Useful when: You want to find and replace across massive projects.

Example:

rg -r "Product" "product" ./docs
Enter fullscreen mode Exit fullscreen mode

Case-sensitive mass edits in seconds. Be careful — there’s no undo.

Why ripgrep Stomps Everything

  • Insanely parallel (multi-threaded)
  • Gitignore respecting (unless you tell it otherwise)
  • PCRE2 fallback if you need heavy regex
  • Speed comparable to tools that only list filenames

VS Code searched 10,000+ files? You bet ripgrep was sweating under the hood, not grep, not ack, not ag.

Bonus Moves

  • --smart-case (-S):

    If pattern has uppercase, do case-sensitive search. Otherwise, case-insensitive.

    No need to think manually.

  • --files:

    Just list files ripgrep would search, without searching.

    (great for dry-runs)

  • --hidden:

    Include hidden files and dirs (.git, .env) without ignoring them.

  • --no-ignore:

    Blast through .gitignore, .ignore, .rgignore, and search EVERYTHING.

  • --vimgrep:

    Format output compatible with Vim/Neovim quickfix lists.

Deep Dive: Multiline Search in ripgrep

One place where beginners trip up: ripgrep is line-oriented by default.

If your pattern spans across multiple lines (for example, matching from orange to kiwi inside a text block), rg won't find it unless you explicitly tell it to.

Why your search might fail:

  • . doesn't match \n by default in regex.

Fix #1: Use --multiline + (?s) flag inside the pattern

  • (?s) makes the dot . match newlines too.
rg -U '(?s)orange.*kiwi'
Enter fullscreen mode Exit fullscreen mode

Fix #2: Use --multiline + --multiline-dotall

  • No need to modify the regex, just allow dot to match newlines globally.
rg -U --multiline-dotall 'orange.*kiwi'
Enter fullscreen mode Exit fullscreen mode

Shortcut:

  • -U is shorthand for --multiline.
  • Best to always prefer -U '(?s)pattern' for short searches.

Summary

Option Purpose
-U Enable multiline matching
(?s) inside regex Make . match newlines
--multiline-dotall Same as (?s), but via flag

TL;DR

ripgrep’s real power isn't just that it finds text fast —

it’s that you can slice, shape, color, invert, filter, extract, or replace what you find at the speed of thought.


I’ve been actively working on a super-convenient tool called LiveAPI.

LiveAPI helps you get all your backend APIs documented in a few minutes

With LiveAPI, you can quickly generate interactive API documentation that allows users to execute APIs directly from the browser.

Image description

If you’re tired of manually creating docs for your APIs, this tool might just make your life easier.

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more