| created | "2025-12-16T00:00:00.000Z" |
| modified | "2025-12-16T00:00:00.000Z" |
| reviewed | "2025-12-16T00:00:00.000Z" |
| name | rg Code Search |
| description | Fast code search using ripgrep (rg) with smart defaults, regex patterns, and file filtering. Use when searching for text patterns, code snippets, or performing multi-file code analysis. |
| allowed-tools | Bash, Read, Grep, Glob |
rg Code Search
Expert knowledge for using rg (ripgrep) as a blazingly fast code search tool with powerful filtering and pattern matching.
Core Expertise
ripgrep Advantages
- Extremely fast (written in Rust)
- Respects
.gitignore automatically
- Smart case-insensitive search
- Recursive by default
- Colorized output
- Multi-line search support
- Replace functionality
Basic Usage
Simple Search
rg pattern
rg "import numpy"
rg function_name
rg -s Pattern
rg -i PATTERN
File Type Filtering
rg pattern -t py
rg pattern -t rs
rg pattern -t js
rg pattern -t md
rg pattern -t py -t rs
rg --type-list
Extension Filtering
rg pattern -g '*.rs'
rg pattern -g '*.{js,ts}'
rg pattern -g '!*.min.js'
Advanced Patterns
Regular Expressions
rg '\bfunction\b'
rg '\btest_\w+'
rg '^import'
rg 'return$'
rg '^class \w+:'
rg 'TODO|FIXME|XXX'
rg '[Ee]rror'
rg '\d{3}-\d{4}'
Multi-line Search
rg -U 'fn.*\{.*\}'
rg -U 'struct.*{[^}]*}'
rg -A 5 pattern
rg -B 3 pattern
rg -C 2 pattern
Output Formatting
rg -l pattern
rg -c pattern
rg --count-matches pattern
rg -n pattern
rg -N pattern
rg --heading pattern
rg --no-heading pattern
rg --vimgrep pattern
rg --json pattern
Advanced Filtering
Path Filtering
rg pattern src/
rg pattern src/ tests/
rg pattern -g '!target/'
rg pattern -g '!{dist,build,node_modules}/'
rg pattern -g '**/test/**'
Content Filtering
rg --files-with-matches "import.*React" | xargs rg "useState"
rg pattern --type-not markdown
rg pattern $(git diff --name-only)
Size and Hidden Files
rg pattern -u
rg pattern -uu
rg pattern -uuu
rg pattern --max-filesize 1M
Code Search Patterns
Finding Definitions
rg '^def \w+\('
rg 'fn \w+\('
rg '^function \w+\('
rg '^\s*class \w+'
rg '^interface \w+'
rg '^type \w+ ='
rg '^struct \w+'
Finding Usage
rg 'functionName\('
rg '\.methodName\('
rg '^import.*module_name'
rg '^use.*crate_name'
rg "^import.*'package'"
Code Quality Checks
rg 'TODO|FIXME|XXX|HACK'
rg -t py '#\s*TODO'
rg -t rs '//\s*TODO'
rg 'console\.log'
rg 'println!'
rg 'print\('
rg 'password.*=|api_key.*='
rg 'eval\('
rg 'exec\('
Testing Patterns
rg '^def test_' -t py
rg '#\[test\]' -t rs
rg "describe\(|it\(" -t js
rg '@skip|@pytest.mark.skip' -t py
rg '#\[ignore\]' -t rs
rg 'test\.skip|it\.skip' -t js
File Operations
Search and Replace
rg pattern --replace replacement
rg pattern -l | xargs sed -i 's/pattern/replacement/g'
fd -e rs | xargs rg pattern --files-with-matches | xargs -I {} sh -c 'vim -c "%s/pattern/replacement/gc" -c "wq" {}'
Integration with Other Tools
rg -l "TODO" | xargs wc -l
rg "function" --files-with-matches | xargs nvim
fd -e py | xargs rg "class.*Test"
fd -e rs | xargs rg "unsafe"
rg -c pattern | awk -F: '{sum+=$2} END {print sum}'
Stats and Analysis
rg pattern --count-matches --no-filename | awk '{sum+=$1} END {print sum}'
rg pattern -o | sort | uniq -c | sort -rn
rg pattern -c | sort -t: -k2 -rn | head -10
Performance Optimization
Speed Tips
rg pattern --max-depth 3
rg pattern -g '*.rs' -t rust
rg pattern -j 4
rg pattern --mmap
rg pattern --no-mmap
Large Codebase Strategies
rg pattern src/
rg pattern -t py -g '!test*'
rg --files > /tmp/files.txt
rg pattern $(cat /tmp/files.txt)
rg pattern -g '!{target,node_modules,dist,build}/'
Best Practices
When to Use rg
- Searching code for patterns
- Finding function/class definitions
- Code analysis and auditing
- Refactoring support
- Security scanning
When to Use grep Instead
- POSIX compatibility required
- Simple one-off searches
- Piped input (stdin)
- System administration tasks
Common Mistakes to Avoid
- Forgetting to escape regex special characters
- Not using
-u flags when searching ignored files
- Not excluding large binary/generated files
- Using grep when rg would be much faster
Quick Reference
Essential Options
| Option | Purpose | Example |
|---|
-t TYPE | File type filter | rg -t py pattern |
-g GLOB | Glob pattern | rg -g '*.rs' pattern |
-i | Case-insensitive | rg -i pattern |
-s | Case-sensitive | rg -s Pattern |
-w | Match whole words | rg -w word |
-l | Files with matches | rg -l pattern |
-c | Count per file | rg -c pattern |
-A N | Lines after | rg -A 5 pattern |
-B N | Lines before | rg -B 3 pattern |
-C N | Context lines | rg -C 2 pattern |
-U | Multi-line | rg -U 'pattern.*' |
-u | Include hidden | rg -u pattern |
--replace | Replace text | rg pattern --replace new |
File Types (Common)
| Type | Extensions |
|---|
-t py | Python (.py, .pyi) |
-t rs | Rust (.rs) |
-t js | JavaScript (.js, .jsx) |
-t ts | TypeScript (.ts, .tsx) |
-t go | Go (.go) |
-t md | Markdown (.md, .markdown) |
-t yaml | YAML (.yaml, .yml) |
-t json | JSON (.json) |
Common Command Patterns
rg '^\s*(def|fn|function)\s+\w+' -t py -t rs -t js
rg '^(import|use|require)' -t py -t rs -t js
rg 'TODO|FIXME|XXX|HACK|BUG'
rg -t py '^def test_' -c
rg -U 'def \w+.*\n(.*\n){50,}' -t py
rg 'password|api_key|secret|token' -i -g '!*.{lock,log}'
This makes rg the preferred tool for fast, powerful code search in development workflows.