Ripgrep Cheatsheet
Core Syntax
rg [OPTIONS] PATTERN [PATH]
Essential Flags
| Flag | Long Form | Purpose |
|---|---|---|
-i | --ignore-case | Case insensitive |
-S | --smart-case | Lowercase = insensitive, uppercase = sensitive |
-w | --word-regexp | Match whole words only |
-v | --invert-match | Lines that do NOT match |
-F | --fixed-strings | Literal string, no regex |
-e PAT | --regexp=PAT | Multiple patterns (OR) |
-f FILE | --file=FILE | Read patterns from file |
-P | --pcre2 | Enable PCRE2 (lookaheads) |
Scope Control
| Flag | Purpose |
|---|---|
-t TYPE | Only this file type (rg --type-list) |
-T TYPE | Exclude this file type |
-g GLOB | Include glob; ! prefix to exclude |
-u | Ignore .gitignore |
-uu | Also search hidden dotfiles |
-uuu | Also search binary files |
--hidden | Just hidden files (not -u) |
--max-depth N | Limit directory depth |
Output Formatting
| Flag | Purpose |
|---|---|
-n | Line numbers (default) |
-N | No line numbers |
-l | Only filenames |
-c | Count matches per file |
-o | Only the matching text |
-A N | N lines after match |
-B N | N lines before match |
-C N | N lines context each side |
-q | Quiet (exit code only) |
--json | Machine-readable NDJSON |
--vimgrep | file:line:col:text format |
--no-heading | Traditional grep format |
--sort path | Sort results by filename |
Quick Pipelines
# Top 10 IPs in Nginx access log
rg -o "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" access.log \
| sort | uniq -c | sort -nr | head -10
# Find and replace across project
rg -l "old_function" src/ | xargs sed -i 's/old_function/new_function/g'
# Files with TODOs, open in vim
rg -l "TODO" | fzf | xargs vim
# Count total errors in all log files
rg -c "ERROR" /var/log/ | awk -F: '{s+=$2} END{print s}'
# Extract all unique exception types from Java logs
rg -o "java\.lang\.\w+Exception" app.log | sort -u
# CI check: block commit if secrets found
rg -q -e "api_key" -e "password" -e "AWS_SECRET" . && echo "BLOCKED" && exit 1
The -u Ladder
| Command | Searches |
|---|---|
rg "p" | Tracked, visible files |
rg -u "p" | + ignored files (node_modules) |
rg -uu "p" | + hidden dotfiles (.env) |
rg -uuu "p" | + binary files |