Skip to main content

Ripgrep Cheatsheet

Core Syntax

rg [OPTIONS] PATTERN [PATH]

Essential Flags

FlagLong FormPurpose
-i--ignore-caseCase insensitive
-S--smart-caseLowercase = insensitive, uppercase = sensitive
-w--word-regexpMatch whole words only
-v--invert-matchLines that do NOT match
-F--fixed-stringsLiteral string, no regex
-e PAT--regexp=PATMultiple patterns (OR)
-f FILE--file=FILERead patterns from file
-P--pcre2Enable PCRE2 (lookaheads)

Scope Control

FlagPurpose
-t TYPEOnly this file type (rg --type-list)
-T TYPEExclude this file type
-g GLOBInclude glob; ! prefix to exclude
-uIgnore .gitignore
-uuAlso search hidden dotfiles
-uuuAlso search binary files
--hiddenJust hidden files (not -u)
--max-depth NLimit directory depth

Output Formatting

FlagPurpose
-nLine numbers (default)
-NNo line numbers
-lOnly filenames
-cCount matches per file
-oOnly the matching text
-A NN lines after match
-B NN lines before match
-C NN lines context each side
-qQuiet (exit code only)
--jsonMachine-readable NDJSON
--vimgrepfile:line:col:text format
--no-headingTraditional grep format
--sort pathSort 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

CommandSearches
rg "p"Tracked, visible files
rg -u "p"+ ignored files (node_modules)
rg -uu "p"+ hidden dotfiles (.env)
rg -uuu "p"+ binary files