ripgrep
Ultra-fast recursive grep alternative.
Official WebsiteFeatures
grepInstallation
brew install ripgrepapt install ripgreppacman -S ripgrepcargo install ripgrepWhy Use ripgrep?
ripgrep (command name: rg) offers overwhelmingly faster speed than the traditional grep command and features optimized for modern development environments.
Overwhelming Speed
Optimized Rust code is 2-10x faster than GNU grep. Instant search even in large codebases.
.gitignore Auto-Recognition
Automatically recognizes .gitignore, .ignore, and .rgignore. Excludes node_modules and build artifacts, searching only needed files.
Full Unicode Support
Auto-detects character encodings like UTF-8 and UTF-16. Supports multilingual search including Japanese.
Compressed File Search
Search directly in compressed files (gz, bz2, xz, lz4) without extraction. Convenient for log analysis.
Basic Usage
Simple Search
# Recursively search current directory and below
rg "search pattern"
# Search specific directory
rg "TODO" src/
# Display filenames only
rg -l "function"
# Case-insensitive search
rg -i "error"Output Example
File paths are purple, line numbers are green, and matched portions are highlighted.
Common Options
| Option | Description | Example |
|---|---|---|
-i | Case insensitive search | rg -i "error" |
-w | Match whole words only | rg -w "log" |
-l | Show only matching file names | rg -l "TODO" |
-c | Count matches | rg -c "console" |
-n | Show line numbers (default) | rg -n "func" |
-A / -B / -C | Show context lines (After/Before/Context) | rg -C 3 "error" |
-t | Specify file type | rg -t py "import" |
-g | Specify files with glob pattern | rg -g "*.ts" "type" |
-v | Show non-matching lines (invert) | rg -v "debug" |
--hidden | Search hidden files | rg --hidden "secret" |
-z | Search compressed files | rg -z "error" logs/ |
-U | Enable multiline matching | rg -U "start.*end" |
Regex Patterns
Using Regular Expressions
# Number patterns
rg "\d{3}-\d{4}" # Phone number format
# Email address pattern
rg "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
# Start and end of line matching
rg "^import" # Lines starting with import
rg ";$" # Lines ending with semicolon
# OR conditions
rg "error|warning|fatal"
# Any character
rg "log.+message" # One or more characters between log and messageLiteral Search (Disable Regex)
# -F option disables regex
rg -F "array[0]" # Search special characters literally
rg -F "**/*.ts" # Search glob pattern literally
rg -F "$HOME" # String containing variable symbolMultiline Search
# -U option enables multiline matching
rg -U "function.*\{[\s\S]*?return" --multiline
# Search inside HTML tags
rg -U "<div class="error">[\s\S]*?</div>"
# Search multiline comment blocks
rg -U "/\*[\s\S]*?\*/"Practical Examples
Code Search
# Search function definitions in TypeScript files
rg -t ts "function\s+\w+"
# Search Python class definitions
rg -t py "^class\s+\w+"
# Find files containing specific imports
rg -l "from react" --type ts
# Find console.log for removal candidates
rg "console\.log" src/
# Search TODO/FIXME comments
rg "(TODO|FIXME|HACK|XXX):" --type-add 'code:*.{js,ts,py,go,rs}'Context Lines Display
# Show 3 lines before and after match
rg -C 3 "error"
# Show 5 lines after match
rg -A 5 "function main"
# Show 2 lines before match
rg -B 2 "return"
# Show separator between files
rg --heading "pattern"File Type Specification
# Check available file types
rg --type-list
# Search specific types only
rg -t js "async" # JavaScript files
rg -t rust "fn main" # Rust files
rg -t py "def " # Python files
rg -t html "<script" # HTML files
# Exclude specific types
rg -T js "config" # Search non-JavaScript files
# Specify multiple types
rg -t ts -t js "import"
# Add custom type
rg --type-add 'web:*.{html,css,js}' -t web "class"Replacement Feature
# Replacement preview (no actual changes)
rg "oldFunction" -r "newFunction"
# Replacement with capture groups
rg "(\w+)_controller" -r "$1Controller"
# Actual replacement combined with sed
rg -l "oldName" | xargs sed -i 's/oldName/newName/g'
# Display file contents while confirming
rg "pattern" -r "replacement" --passthrufzf Integration
Interactive Search
# Filter ripgrep results with fzf
rg --line-number --no-heading . | fzf
# Select files with preview
rg -l "pattern" | fzf --preview 'bat --color=always {}'
# Open file from search results
rg -l "TODO" | fzf --preview 'rg --color=always "TODO" {}' | xargs -o vim
# Real-time search (no initial query)
: | fzf --bind 'change:reload:rg --line-number {q} || true' --ansi --disabledUseful Function Definitions
# Interactive grep using fzf
rgf() {
rg --color=always --line-number --no-heading "$@" |
fzf --ansi \
--delimiter : \
--preview 'bat --color=always {1} --highlight-line {2}' \
--preview-window 'up,60%,border-bottom,+{2}+3/3,~3' \
--bind 'enter:become(vim {1} +{2})'
}
# Interactive search in current directory
rgi() {
rg --color=always --line-number "" |
fzf --ansi --query="$1" \
--delimiter : \
--preview 'bat --color=always {1} --highlight-line {2}'
}Configuration and Customization
Configuration File
Specify a configuration file using ~/.ripgreprc or the environment variable RIPGREP_CONFIG_PATH.
# Smart case (case insensitive when pattern is all lowercase)
--smart-case
# Search hidden files
--hidden
# Always exclude certain directories
--glob=!.git/
--glob=!node_modules/
--glob=!vendor/
--glob=!dist/
--glob=!build/
--glob=!*.min.js
# Maximum column width setting
--max-columns=150
# Always enable color output
--color=auto
# Show line numbers
--line-number
# Display filename as header
--heading
# Set match highlight colors
--colors=line:fg:yellow
--colors=match:bg:yellow
--colors=match:fg:blackEnvironment Variable Setup
# Specify config file path
export RIPGREP_CONFIG_PATH="$HOME/.ripgreprc"
# Color customization
export RIPGREP_CONFIG_PATH="$HOME/.config/ripgrep/config"Project-Specific Settings (.rgignore)
# Place in project root
# Same format as .gitignore
# Build artifacts
dist/
build/
out/
# Dependencies
node_modules/
vendor/
# Log files
*.log
logs/
# Temporary files
*.tmp
*.temp
.cache/
# Test coverage
coverage/Shell Aliases
# ~/.bashrc or ~/.zshrc
# Replace grep with rg
alias grep='rg'
# Common option combinations
alias rgg='rg --hidden --glob "!.git"' # Search hidden files (exclude .git)
alias rgf='rg --files-with-matches' # File names only
alias rgc='rg --count' # Count only
alias rgi='rg --ignore-case' # Case insensitive
# Search with context
alias rg3='rg -C 3' # Show 3 lines before/after
alias rg5='rg -C 5' # Show 5 lines before/afterComparison with grep
| Feature | grep | ripgrep |
|---|---|---|
| Search Speed | Standard | 2-10x faster |
| Recursive Search | Requires grep -r | Recursive by default |
| .gitignore Support | - | Auto-detection |
| Unicode Support | Limited | Full support |
| Compressed File Search | Use zgrep | -z option |
| Color Output | Requires --color | Enabled by default |
| File Type Specification | --include pattern | Easy with -t |
| Multiline Match | Requires -P (PCRE) | Supported with -U |
| Replacement Preview | - | -r option |
| Portability | Standard on most systems | Requires separate installation |
Benchmark Example (Linux Kernel Source Search)
Tips
- •Use
rg --type-listto see all supported file types. Over 200 types are supported. - •Setting
--smart-caseenables smart mode: lowercase-only patterns are case insensitive, while patterns with uppercase characters are case sensitive. - •Use
--no-ignoreto ignore .gitignore, or--no-ignore-vcsto ignore only VCS-related ignore files. - •Vim users can replace the
:grepcommand with rg for a better experience. Addset grepprg=rg\ --vimgrepto your .vimrc. - •The
--jsonoption outputs in JSON format, useful for integration with other tools. - •When searching large files, you can limit file size with
--max-filesize 1M. - •Binary files are automatically skipped. Use the
--binaryoption if you want to search them.
Related Articles
fd - Fast find Alternative
Simple, fast alternative to find command
grep Command: Practical Examples & Usage Guide
Master grep with practical examples for developers. Learn pattern matching, regex, and real-world use cases.
fzf - Command-line Fuzzy Finder
General-purpose command-line fuzzy finder