Drill: Code Search with ripgrep (rg)¶
Goal¶
Use ripgrep for fast code search with file type filters, context lines, multiline patterns, and glob-based exclusions.
Setup¶
- Install ripgrep (
apt install ripgrep,brew install ripgrep, or download from GitHub) - A codebase or directory to search
Commands¶
Basic search:
Case-insensitive search:
Search with context lines (before/after):
Filter by file type:
List available types:
Search with glob patterns:
Search for a fixed string (no regex):
Show only filenames:
Count matches per file:
Search for multiline patterns:
Invert match (lines NOT containing pattern):
Search with word boundaries:
Use custom file type definitions:
What to Look For¶
- rg respects .gitignore by default (use
--no-ignoreto override) - File type filters save time over manual glob patterns
-Uenables multiline mode for patterns spanning line boundaries--jsonoutput can be piped to jq for programmatic processing
Common Mistakes¶
- Forgetting that rg skips .gitignored files by default
- Not using
-Ffor literal strings containing regex metacharacters - Using
-g '*.py'without the quotes, causing shell glob expansion - Not using
-wand getting false positives from partial word matches
Cleanup¶
No cleanup needed. rg is a read-only search tool.