コンテンツにスキップ

Ripgrep (rg) Cheatsheet

Ripgrep (rg) Cheatsheet

Installation

PlatformCommand
Ubuntu/Debiansudo apt update && sudo apt install ripgrep
Ubuntu/Debian (latest)curl -LO https://github.com/BurntSushi/ripgrep/releases/download/13.0.0/ripgrep_13.0.0_amd64.deb && sudo dpkg -i ripgrep_13.0.0_amd64.deb
Fedorasudo dnf install ripgrep
RHEL/CentOSsudo yum install epel-release && sudo yum install ripgrep
Arch Linuxsudo pacman -S ripgrep
Alpine Linuxapk add ripgrep
macOS (Homebrew)brew install ripgrep
macOS (MacPorts)sudo port install ripgrep
Windows (Chocolatey)choco install ripgrep
Windows (Scoop)scoop install ripgrep
Windows (Winget)winget install BurntSushi.ripgrep.MSVC
Snap (Universal)sudo snap install ripgrep --classic
From Source (Cargo)cargo install ripgrep
Verify Installationrg --version

Basic Commands

CommandDescription
rg "pattern"Search for pattern in current directory recursively
rg "pattern" file.txtSearch for pattern in specific file
rg "pattern" /path/to/dirSearch for pattern in specific directory
rg -i "pattern"Case-insensitive search
rg -s "Pattern"Case-sensitive search (override smart case)
rg -S "pattern"Smart case (case-insensitive if all lowercase)
rg -F "exact.string"Search for exact literal string (no regex)
rg -w "word"Search for whole word matches only
rg -l "pattern"Show only filenames containing matches
rg --files-without-match "pattern"Show only filenames without matches
rg -c "pattern"Count matches per file
rg -n "pattern"Show line numbers (enabled by default)
rg -N "pattern"Hide line numbers
rg --column "pattern"Show column numbers of matches
rg -C 3 "pattern"Show 3 lines of context before and after match
rg -B 2 "pattern"Show 2 lines before match
rg -A 2 "pattern"Show 2 lines after match
rg --hidden "pattern"Search hidden files and directories
rg -u "pattern"Search all files including ignored ones
rg -uuu "pattern"Search everything (hidden + ignored + binary)

Advanced Usage

CommandDescription
rg -t py "pattern"Search only Python files
rg -T js "pattern"Exclude JavaScript files from search
rg -g "*.{js,ts}" "pattern"Search using glob pattern
rg -g "!*.min.js" "pattern"Exclude files matching glob pattern
rg --type-listList all supported file types
rg --type-add 'web:*.{html,css,js}' -t web "pattern"Define and use custom file type
rg -o "pattern"Show only matched text, not entire line
rg "pattern" -r "replacement"Show matches with replacement text
rg --json "pattern"Output results in JSON format
rg -U "pattern.*\n.*pattern2"Multiline search mode
rg -P "(?<=@)\w+"Use PCRE2 regex engine for advanced patterns
rg -f patterns.txtRead patterns from file (one per line)
rg --stats "pattern"Show search statistics after results
rg -j 8 "pattern"Use 8 threads for parallel search
rg --sort path "pattern"Sort results by file path
rg --max-filesize 1M "pattern"Skip files larger than 1MB
rg --max-columns 500 "pattern"Skip lines longer than 500 characters
rg -L "pattern"Follow symbolic links
rg --no-ignore-vcs "pattern"Don’t respect .gitignore files
rg --filesList all files that would be searched
`rg —color always “pattern” \less -R`
rg --colors 'match:fg:yellow' "pattern"Customize match color
rg --heading "pattern"Group results by file with headings
rg --no-heading "pattern"Show results inline without file grouping
rg "\bfunction\b" -A 5Search for word boundary and show 5 lines after

Configuration

Ripgrep can be configured using a configuration file to set default options.

Configuration File Locations

# Linux/macOS
~/.config/ripgrep/config
~/.ripgreprc

# Windows
%APPDATA%\ripgrep\config

# Set custom location via environment variable
export RIPGREP_CONFIG_PATH=/path/to/config

Sample Configuration File

# ~/.ripgreprc

# Always use smart case
--smart-case

# Always show line numbers
--line-number

# Always show column numbers
--column

# Set default context lines
--context=2

# Always search hidden files
--hidden

# Custom ignore patterns
--glob=!.git/*
--glob=!node_modules/*
--glob=!*.min.js
--glob=!*.map
--glob=!dist/*
--glob=!build/*
--glob=!target/*

# Custom colors
--colors=match:fg:yellow
--colors=match:style:bold
--colors=path:fg:green
--colors=line:fg:cyan

# Performance settings
--threads=8
--max-filesize=2M

# Output formatting
--heading
--max-columns-preview=150

Environment Variables

# Set config file location
export RIPGREP_CONFIG_PATH="$HOME/.ripgreprc"

# Disable config file
export RIPGREP_CONFIG_PATH=""

Common Use Cases

Use Case: Finding Function Definitions

# Find function definitions in Python files
rg -t py "^def \w+\("

# Find function definitions with context
rg -t py "^def \w+\(" -A 3

# Find all class definitions in JavaScript
rg -t js "^class \w+" --heading

Use Case: Searching Through Logs

# Find errors in log files
rg -i "error|exception|fatal" /var/log/

# Find errors with timestamps and context
rg "ERROR" --glob "*.log" -C 2

# Count error occurrences per log file
rg -c "ERROR" --glob "*.log" --sort path

# Search compressed logs
rg -z "pattern" /var/log/archive/

Use Case: Code Refactoring

# Find all usages of deprecated API
rg "oldFunction\(" -t js -t ts

# Find TODO comments across codebase
rg "TODO|FIXME|HACK" --heading

# Find hardcoded credentials (security audit)
rg -i "(password|api[_-]?key|secret|token)\s*=\s*['\"]" --type-add 'config:*.{yml,yaml,json,env}' -t config

# Find imports of specific module
rg "^import.*from ['\"]module-name['\"]" -t js

Use Case: Finding Configuration Values

# Search for config value across all config files
rg "database_host" --type-add 'config:*.{yml,yaml,json,conf,ini,toml}' -t config

# Find environment variables
rg "export \w+=" --glob "*.sh" --glob "*.bash"

# Search in dotfiles (hidden files)
rg --hidden "pattern" ~/

Use Case: Working with Large Codebases

# Search with performance optimization
rg -j 16 --mmap "pattern" /large/codebase

# Search excluding test files
rg "pattern" -g "!**/*test*" -g "!**/*spec*"

# Search only recently modified files (combine with find)
find . -type f -mtime -7 -print0 | xargs -0 rg "pattern"

# Search with file size limit
rg --max-filesize 500K "pattern" /large/codebase

Best Practices

  • Use file type filters (-t, -T) instead of glob patterns when possible for better performance and readability
  • Leverage smart case (-S) as default - it searches case-insensitively when pattern is lowercase, case-sensitively otherwise
  • Create a .ripgreprc config file to set your preferred defaults and avoid repetitive command-line flags
  • Use .ignore files in project roots to exclude directories like node_modules, dist, build without affecting git
  • Prefer literal search (-F) when searching for exact strings containing regex metacharacters to avoid escaping
  • Use --stats during optimization to understand search performance and identify bottlenecks in large codebases
  • Combine with other tools like xargs, fzf, or sed for powerful text processing pipelines
  • Use --files first when debugging to verify which files will be searched before running the actual search
  • Set thread count (-j) based on your CPU cores for optimal performance on large searches
  • Use multiline mode (-U) sparingly as it’s slower; prefer multiple searches when possible

Troubleshooting

IssueSolution
No results found but pattern existsCheck if files are being ignored; use -u or -uuu to search ignored/hidden files
Binary file matches not shownUse -a or --text to search binary files as text, or -uuu to search everything
Search is too slowReduce thread count with -j 1 to check if parallelism is causing issues, or use --max-filesize to skip large files
Regex pattern not workingVerify regex syntax; use -F for literal search or -P for PCRE2 engine with advanced features
Colors not showing in outputUse --color always when piping to other commands, or check terminal color support
.gitignore not being respectedEnsure you’re in a git repository; use --debug to see which ignore files are being used
Memory usage too highDisable memory mapping with --no-mmap or reduce --mmap-threshold value
Results not sorted as expectedUse --sort path, --sort modified, or --sort accessed (note: disables parallelism)
Config file not being loadedCheck RIPGREP_CONFIG_PATH environment variable and verify file exists at expected location
Symbolic links not followedUse -L or --follow to follow symbolic links during search
Pattern matches too muchUse word boundaries (\b), start/end anchors (^, $), or -w for whole word matches
Can’t search in specific file typeCheck --type-list for available types or define custom type with --type-add

Quick Reference: Common Patterns

PatternDescriptionExample
\bword\bWord boundariesrg "\bfunction\b"
^patternStart of linerg "^import"
pattern$End of linerg ";\s*$"
`pattern1\pattern2`OR operator
[0-9]{3}Digit repetitionrg "[0-9]{3}-[0-9]{4}"
(?i)patternCase insensitive flagrg "(?i)todo"
(?P<name>\w+)Named capture grouprg "(?P<email>\w+@\w+\.\w+)"
.*Any charactersrg "start.*end"
\s+Whitespacerg "=\s+\w+"