| created | "2025-12-16T00:00:00.000Z" |
| modified | "2026-05-04T00:00:00.000Z" |
| reviewed | "2026-04-25T00:00:00.000Z" |
| name | rg-code-search |
| description | ripgrep (rg) fast code search: smart defaults, regex, file filtering. Use when searching for text patterns, code snippets, or doing multi-file analysis. |
| user-invocable | false |
| allowed-tools | Bash(rg *), Read, Grep, Glob |
| model | sonnet |
rg Code Search
Expert knowledge for using rg (ripgrep) as a blazingly fast code search tool with powerful filtering and pattern matching.
When to Use This Skill
| Use this skill when... | Use fd-file-finding instead when... |
|---|
| Searching file contents for text or regex patterns | Searching for files by name, extension, or path |
Filtering matches by file type (-t py, -t js) | Filtering files by mtime, size, or -type |
| Multi-line pattern matching across source files | Locating files to feed into another tool |
| Use this skill when... | Use binary-analysis instead when... |
|---|
| Searching source code or text-encoded files | Extracting strings from compiled binaries or firmware |
| Auditing repos for hardcoded patterns in tracked files | Hunting for credentials inside ELF, Mach-O, or .bin blobs |
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 -x rg "class.*Test" {}
fd -e rs -x 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
Tips for Effective Searches
- Escape regex special characters in patterns
- Use
-u flags when searching ignored/hidden files
- Exclude large binary/generated files with
--glob '!vendor'
- Prefer rg over grep for speed and smart defaults
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.