| name | ripgrep |
| description | Use when searching text in files, codebases, books, or documents. Use when finding files by pattern, searching large files that are too big to read fully, extracting specific content from many files, or when grep/find is too slow. Triggers on "search for", "find occurrences", "look for pattern", "search in files". |
Ripgrep (rg) - Fast Text Search Tool
Overview
Ripgrep is a line-oriented search tool that recursively searches directories for regex patterns. It's 10-100x faster than grep and respects .gitignore by default. Use it instead of grep, find, or manually reading large files.
Core principle: When you need to find text in files, use ripgrep. Don't read entire files into context when you can search them.
When to Use
Use ripgrep when:
- Searching for text patterns across a codebase or directory
- Finding all occurrences of a function, variable, or string
- Searching through books, documentation, or large text files
- Files are too large to read fully into context
- Looking for specific content in many files at once
- Finding files that contain (or don't contain) certain patterns
- Extracting matching lines for analysis
Don't use when:
- You need the full file content (use Read tool)
- Simple glob pattern matching for filenames only (use Glob tool)
- You need structured data extraction (consider jq, awk)
Quick Reference
| Task | Command |
|---|
| Basic search | rg "pattern" [path] |
| Case insensitive | rg -i "pattern" |
| Smart case (auto) | rg -S "pattern" |
| Whole word only | rg -w "word" |
| Fixed string (no regex) | rg -F "literal.string" |
| Show context lines | rg -C 3 "pattern" (3 before & after) |
| Show line numbers | rg -n "pattern" (default in tty) |
| Only filenames | rg -l "pattern" |
| Files without match | rg --files-without-match "pattern" |
| Count matches | rg -c "pattern" |
| Only matching part | rg -o "pattern" |
| Invert match | rg -v "pattern" |
| Multiline search | rg -U "pattern.*\nmore" |
File Filtering
By File Type
Ripgrep has built-in file type definitions. Use -t to include, -T to exclude:
rg -t py "def main"
rg -t js -t ts "import"
rg -T test "function"
rg --type-list
Common types: py, js, ts, rust, go, java, c, cpp, rb, php, html, css, json, yaml, md, txt, sh
By Glob Pattern
rg -g "*.tsx" "useState"
rg -g "!node_modules/**" "pattern"
rg -g "src/**" "pattern"
rg -g "*.js" -g "*.ts" "pattern"
rg --iglob "*.JSON" "pattern"
By File Size
rg --max-filesize 1M "pattern"
Directory Control
rg --max-depth 2 "pattern"
rg --hidden "pattern"
rg -L "pattern"
rg --no-ignore "pattern"
rg -u "pattern"
rg -uu "pattern"
rg -uuu "pattern"
Context Options
rg -A 5 "pattern"
rg -B 5 "pattern"
rg -C 5 "pattern"
rg --passthru "pattern"
Output Formats
rg -l "pattern"
rg --files-without-match "pattern"
rg -c "pattern"
rg --count-matches "pattern"
rg -o "pattern"
rg --json "pattern"
rg --vimgrep "pattern"
rg --stats "pattern"
Regex Patterns
Ripgrep uses Rust regex syntax by default:
rg "foo|bar"
rg "[0-9]+"
rg "[a-zA-Z_][a-zA-Z0-9_]*"
rg "\bword\b"
rg "colou?r"
rg "go+gle"
rg "ha*"
rg "x{2,4}"
rg "(foo|bar)baz"
rg -P "(?<=prefix)content"
rg -P "content(?=suffix)"
Multiline Matching
rg -U "start.*\nend"
rg -U --multiline-dotall "start.*end"
rg -U "function\s+\w+\([^)]*\)\s*\{"
Replacement (Preview Only)
Ripgrep can show what replacements would look like (doesn't modify files):
rg "old" -r "new"
rg "(\w+)@(\w+)" -r "$2::$1"
rg "pattern" -r ""
Searching Special Files
Compressed Files
rg -z "pattern" file.gz
rg -z "pattern" archive.tar.gz
Binary Files
rg --binary "pattern"
rg -a "pattern"
Large Files
For files too large to read into context:
rg "specific pattern" large_file.txt
rg -m 10 "pattern" huge_file.log
rg -b "pattern" large_file.txt
rg "pattern" large_file.txt | head -100
Performance Tips
- Be specific with paths - Don't search from root when you know the subdir
- Use file types -
-t py is faster than -g "*.py"
- Use fixed strings -
-F when you don't need regex
- Limit depth -
--max-depth when you know structure
- Let gitignore work - Don't use
--no-ignore unless needed
- Use word boundaries -
-w is optimized
Common Patterns
Find function definitions
rg "def \w+\(" -t py
rg "(function|const|let|var)\s+\w+\s*=" -t js -t ts
rg "^\s*(async\s+)?function" -t js
rg "^func\s+\w+" -t go
Find imports/requires
rg "^(import|from)\s+" -t py
rg "^(import|require\()" -t js
rg "^import\s+" -t go
Find TODO/FIXME comments
rg "(TODO|FIXME|HACK|XXX):"
Find error handling
rg "except\s+\w+:" -t py
rg "\.catch\(|catch\s*\(" -t js
Find class definitions
rg "^class\s+\w+" -t py
rg "^(export\s+)?(default\s+)?class\s+\w+" -t js -t ts
Search in books/documents
rg "^(Chapter|CHAPTER)\s+\d+" book.txt
rg '"[^"]{20,}"' document.txt
rg -C 2 "keyword" book.txt
Combining with Other Tools
rg --files | xargs rg "pattern"
rg -c "pattern" | sort -t: -k2 -rn
rg -l "pattern" | xargs code
rg -o "\b[A-Z]{2,}\b" | sort -u
rg -f patterns.txt
Exit Codes
| Code | Meaning |
|---|
| 0 | Matches found |
| 1 | No matches found |
| 2 | Error occurred |
Useful for scripting:
if rg -q "pattern" file.txt; then
echo "Found"
fi
Common Mistakes
| Mistake | Fix |
|---|
| Pattern has special chars | Use -F for fixed strings or escape: rg "foo\.bar" |
| Can't find hidden files | Add --hidden or -uu |
| Missing node_modules | Add --no-ignore (but it's usually right to skip) |
| Regex too complex | Try -P for PCRE2 with lookahead/lookbehind |
| Output too long | Use -m N to limit, or -l for just filenames |
| Binary file skipped | Add --binary or -a for text mode |
| Need to see full line | Remove -o (only-matching) flag |
When to Prefer Other Tools
| Task | Better Tool |
|---|
| Structured JSON queries | jq |
| Column-based text processing | awk |
| Stream editing/substitution | sed (actually modifies files) |
| Find files by name only | fd or find |
| Simple file listing | ls or glob |
| Full file content needed | Read tool |