Ripgrep Quick Start Guide
Ripgrep (rg
) is a line-oriented search tool that recursively searches your current directory for a regex pattern. It's designed to be fast, user-friendly, and respectful of your .gitignore
rules by default.
Installation
On macOS
brew install ripgrep
On Ubuntu/Debian
sudo apt-get install ripgrep
On Windows
# Using Chocolatey
choco install ripgrep
# Using Scoop
scoop install ripgrep
From GitHub
Download the latest release from GitHub.
Basic Usage
Simple Search
Search for a pattern in the current directory:
rg "pattern"
Case Insensitive Search
rg -i "pattern"
Search in Specific File Types
rg "pattern" -t js # Search only JavaScript files
rg "pattern" -t py # Search only Python files
Exclude File Types
rg "pattern" -T js # Exclude JavaScript files
Search in Specific Directories
rg "pattern" path/to/directory
Search Multiple Patterns
rg "pattern1|pattern2"
Advanced Features
Show Context Around Matches
rg -A 2 "pattern" # 2 lines after match
rg -B 2 "pattern" # 2 lines before match
rg -C 2 "pattern" # 2 lines before and after match
Count Matches
rg -c "pattern"
Show Only Filenames
rg -l "pattern" # Files with matches
rg -L "pattern" # Files without matches
Fixed String Search (No Regex)
rg -F "pattern with [special] characters"
Include Hidden and Ignored Files
rg --hidden "pattern" # Include hidden files
rg --no-ignore "pattern" # Ignore .gitignore rules
rg --no-ignore-vcs "pattern" # Ignore VCS ignore files
Replace Text
rg "pattern" --replace "replacement"
Show Line Numbers
rg -n "pattern" # This is actually the default
Search for Whole Words
rg -w "word"
Useful Combinations
Find All TODOs in Your Codebase
rg "TODO|FIXME" --color=always | less -R
Search in ZIP Files
rg -z "pattern" file.zip
Search with Glob Patterns
rg "pattern" --glob "*.{js,ts}" # Search in JS and TS files
rg "pattern" --glob "!*.min.js" # Exclude minified JS files
Output in JSON Format
rg "pattern" --json
Configuration
You can create a configuration file at ~/.ripgreprc
with your preferred default options:
--smart-case
--colors=match:fg:yellow
--colors=match:style:bold
--max-columns=150
--max-columns-preview
Then set the RIPGREP_CONFIG_PATH
environment variable:
export RIPGREP_CONFIG_PATH="$HOME/.ripgreprc"
Tips and Tricks
- Use
--debug
to see why a file is being searched or ignored. - Combine with other tools:
rg "pattern" | grep "filter" | sort
- Use
-o
to show only the matching part of the line. - For very large codebases, try
--mmap
for potentially better performance.
Resources
Common Issues
Pattern Not Found
- Check if you need case-insensitive search (
-i
) - Ensure you're not being blocked by
.gitignore
rules (use--no-ignore
) - Try using fixed strings (
-F
) if your pattern has special regex characters
Performance Issues
- Try limiting the search depth with
--max-depth
- Use type filters (
-t
) to search only specific file types - For very large files, consider using
--pre
to process files before searching