ripgrep Cheatsheet
Quick reference for recursive text searches with the ripgrep command
The `ripgrep` command (`rg`) searches files recursively, respects ignore files, and filters results by type or glob. This cheatsheet covers common `rg` commands for code searches, logs, context output, and replacement previews.
Basic Search
Search files and directories for matching text.
| Command | Description |
|---|---|
rg "pattern" | Search recursively from the current directory |
rg "pattern" file.txt | Search a single file |
rg "pattern" dir/ | Search a specific directory |
rg "pattern" file1 file2 | Search specific files |
rg --version | Show the installed ripgrep version |
File Type Filters
Limit searches to known file types.
| Command | Description |
|---|---|
rg -t py "pattern" | Search only Python files |
rg -t js "pattern" | Search only JavaScript files |
rg -t markdown "pattern" | Search only Markdown files |
rg -T js "pattern" | Exclude JavaScript files |
rg --type-list | Show available file type names |
Glob Filters
Include or exclude paths with glob patterns.
| Command | Description |
|---|---|
rg -g '*.log' "error" | Search only .log files |
rg -g '*.conf' "listen" /etc | Search matching config files |
rg -g '!*.min.js' "console.log" | Exclude minified JavaScript files |
rg -g '!node_modules/' "TODO" | Exclude a directory |
rg -g '*.md' -g '!README.md' "pattern" | Combine include and exclude globs |
Case and Literal Search
Control case matching and regex handling.
| Command | Description |
|---|---|
rg -i "warning" | Case-insensitive search |
rg -S "warning" | Smart case search |
rg -s "Warning" | Force case-sensitive search |
rg -F "price[0]" | Search for a fixed string |
rg -w "id" | Match whole words only |
Counts and File Lists
Summarize matches or print filenames.
| Command | Description |
|---|---|
rg -c "error" | Count matching lines per file |
rg --count-matches "error" | Count individual matches per file |
rg -l "error" | List files with matches |
rg --files-without-match "error" | List files without matches |
rg --stats "error" | Print search statistics |
Context Output
Show lines around each match.
| Command | Description |
|---|---|
rg -C 3 "panic" | Show 3 lines before and after |
rg -A 2 "error" | Show 2 lines after |
rg -B 2 "error" | Show 2 lines before |
rg -n "error" | Show line numbers |
rg -N "error" | Hide line numbers |
Patterns and Regex
Search with multiple patterns and regex features.
| Command | Description |
|---|---|
rg -e "error" -e "warning" | Match either pattern |
rg -e "--force" | Search for a pattern starting with - |
rg 'error|warning' | Use regex alternation |
rg '^server' | Match lines starting with server |
rg 'listen$' | Match lines ending with listen |
Hidden and Ignored Files
Search paths that rg skips by default.
| Command | Description |
|---|---|
rg --hidden "api_key" | Include hidden files and directories |
rg --no-ignore "TODO" | Ignore .gitignore, .ignore, and .rgignore |
rg --hidden --no-ignore "password" | Search hidden and ignored files |
rg -u "pattern" | Reduce ignore filtering by one level |
rg -uuu "pattern" | Search almost everything, including binary files |
Replacement Preview
Preview changed output without editing files.
| Command | Description |
|---|---|
rg "old" -r "new" | Preview replacing old with new |
rg '(foo)(bar)' -r '$2$1' | Reorder capture groups in output |
rg -o 'v[0-9]+\.[0-9]+\.[0-9]+' | Print only matched version strings |
rg -o -r '$1' 'version = "([^"]+)"' | Extract a captured value |
rg "foo" -r "bar" | Preview before using sed or an editor |
Troubleshooting
Quick checks for common rg issues.
| Issue | Check |
|---|---|
| File is missing from results | Run rg --debug "pattern" |
| Hidden files are skipped | Add --hidden |
| Ignored files are skipped | Add --no-ignore |
| Glob does not work | Quote it, for example -g '*.conf' |
Pattern starts with - | Use rg -e "--flag" |
Common Options
Useful flags to remember.
| Option | Description |
|---|---|
-t TYPE | Search only a file type |
-T TYPE | Exclude a file type |
-g GLOB | Include or exclude paths by glob |
-i | Ignore case |
-S | Smart case |
-F | Fixed string search |
-w | Whole-word match |
-l | List matching files |
--files-without-match | List non-matching files |
-C N | Show context lines |
-r TEXT | Preview replacement output |
--hidden | Include hidden files |
--no-ignore | Ignore ignore-file rules |
Related Guides
Use these guides for full command workflows.
| Guide | Description |
|---|---|
| ripgrep Command in Linux | Full rg tutorial with practical examples |
| grep Command in Linux | Standard text search with GNU grep |
| Grep Exclude | Exclude files, directories, and patterns with grep |
| find Files in Linux | Locate files by metadata and path |
| sed Find and Replace | Replace text after previewing matches |