Skip to main content

ripgrep Cheatsheet

By Dejan Panovski Updated on Download PDF

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.

Search files and directories for matching text.

CommandDescription
rg "pattern"Search recursively from the current directory
rg "pattern" file.txtSearch a single file
rg "pattern" dir/Search a specific directory
rg "pattern" file1 file2Search specific files
rg --versionShow the installed ripgrep version

File Type Filters

Limit searches to known file types.

CommandDescription
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-listShow available file type names

Glob Filters

Include or exclude paths with glob patterns.

CommandDescription
rg -g '*.log' "error"Search only .log files
rg -g '*.conf' "listen" /etcSearch 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

Control case matching and regex handling.

CommandDescription
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.

CommandDescription
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.

CommandDescription
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.

CommandDescription
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.

CommandDescription
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.

CommandDescription
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.

IssueCheck
File is missing from resultsRun rg --debug "pattern"
Hidden files are skippedAdd --hidden
Ignored files are skippedAdd --no-ignore
Glob does not workQuote it, for example -g '*.conf'
Pattern starts with -Use rg -e "--flag"

Common Options

Useful flags to remember.

OptionDescription
-t TYPESearch only a file type
-T TYPEExclude a file type
-g GLOBInclude or exclude paths by glob
-iIgnore case
-SSmart case
-FFixed string search
-wWhole-word match
-lList matching files
--files-without-matchList non-matching files
-C NShow context lines
-r TEXTPreview replacement output
--hiddenInclude hidden files
--no-ignoreIgnore ignore-file rules

Use these guides for full command workflows.

GuideDescription
ripgrep Command in LinuxFull rg tutorial with practical examples
grep Command in LinuxStandard text search with GNU grep
Grep ExcludeExclude files, directories, and patterns with grep
find Files in LinuxLocate files by metadata and path
sed Find and ReplaceReplace text after previewing matches