linuxkernel.com

ripgrep cheat sheet

replaces grep

Search code recursively and blazingly fast; respects .gitignore by default.

Covers ripgrep 15.1.0. Click any command for details & copy · press / to search.

Basic Search

rg 'pattern'Recursively search the current directory for pattern (respects .gitignore, skips hidden/binary files)
rg 'pattern' path/to/dirSearch a specific directory recursively
rg 'pattern' file.txtSearch a single file
rg -e 'pattern1' -e 'pattern2'Search for multiple patterns simultaneously; lines matching any pattern are printed
rg -f patterns.txtRead patterns from a file, one per line
rg -w 'word'Match whole words only (adds word-boundary anchors)
rg -x 'exact line'Match entire lines only (line must equal the pattern exactly)
rg -v 'pattern'Invert match: print lines that do NOT match the pattern
rg -m NUM 'pattern'Stop after NUM matching lines per file

Case Sensitivity

rg 'Pattern'Default: case-sensitive search
rg -i 'pattern'Case-insensitive search (matches Error, ERROR, error, etc.)
rg -S 'pattern'Smart-case: case-insensitive unless the pattern contains an uppercase letter
rg -s 'Pattern'Force case-sensitive search (overrides -i or -S if set in config)

Regex and Fixed Strings

rg 'regex\d+pattern'Default: pattern is a regex (Rust regex syntax)
rg -F 'literal.string'Treat pattern as a fixed/literal string, disabling regex interpretation
rg -P 'pattern'Use PCRE2 regex engine (enables lookahead, lookbehind, backreferences)
rg -U 'start.*?end'Enable multiline matching (dot matches newlines when combined with --multiline-dotall)
rg --multiline-dotall 'start.*end'Make dot (.) match newlines in multiline mode (requires -U)

File Type Filtering

rg -t TYPE 'pattern'Search only files of the named type (e.g. rust, py, js, go, css, json, yaml)
rg -T TYPE 'pattern'Exclude files of the named type from the search
rg --type-listList all built-in file types and their glob patterns (100+ types supported)
rg --type-add 'web:*.{html,css,js}' -t web 'pattern'Define a custom file type for this invocation, then filter by it
rg -t rust -t toml 'pattern'Filter by multiple types simultaneously (union)

Glob Filtering

rg -g 'GLOB' 'pattern'Include only files matching the glob pattern
rg -g '!GLOB' 'pattern'Exclude files matching the glob pattern (prefix glob with !)
rg -g '*.{ts,tsx}' 'pattern'Match files with any of the listed extensions
rg -g '!node_modules/**' 'pattern'Exclude an entire directory tree from the search
rg --iglob 'GLOB' 'pattern'Case-insensitive glob match

Hidden Files and Ignored Files

rg -. 'pattern'Search hidden files and directories (dotfiles and dot-dirs)
rg --hidden 'pattern'Alias for -.: search hidden files and directories
rg --no-ignore 'pattern'Disable all .gitignore / .ignore file filtering
rg -u 'pattern'One -u: disable .gitignore filtering. Two (-uu): also search hidden files. Three (-uuu): also search binary files
rg --hidden --no-ignore 'pattern'Search everything: hidden files, ignored files, all of it
rg -L 'pattern'Follow symbolic links during directory traversal

Context Lines

rg -C NUM 'pattern'Show NUM lines of context before and after each match
rg -A NUM 'pattern'Show NUM lines after each match
rg -B NUM 'pattern'Show NUM lines before each match
rg --passthru 'pattern'Print all lines, highlighting matches in context (like grep -C $(wc -l) but efficient)
rg --context-separator '---' -C 2 'pattern'Change the separator string between context groups (default: --)

Output Formatting

rg -n 'pattern'Show line numbers (default when stdout is a tty and multiple files are searched)
rg -N 'pattern'Suppress line numbers
rg -H 'pattern'Always print filename with each match (default for multi-file searches)
rg -I 'pattern'Never print filenames with matches
rg --heading 'pattern'Group matches by file, printing the filename once as a header above its matches
rg --no-heading 'pattern'Print filename as a prefix on each match line (grep-style output, good for piping)
rg --column 'pattern'Print column number of each match in addition to line number
rg -o 'pattern'Print only the matching portion of each line, one match per line
rg -M NUM 'pattern'Truncate lines longer than NUM characters
rg -M NUM --max-columns-preview 'pattern'Truncate long lines and show a preview of the truncated content
rg -p 'pattern'Pretty-print: force colors and heading even when piped (alias for --pretty)
rg --json 'pattern'Output results as JSON Lines (machine-readable; useful for editor integrations)
rg --vimgrep 'pattern'Output in Vim quickfix format: file:line:col:match
rg -q 'pattern'Quiet mode: exit 0 if any match found, exit 1 if none, print nothing
rg --trim 'pattern'Strip leading whitespace from each matched line in output

Replace and Preview

rg 'pattern' -r 'replacement'Replace matches in OUTPUT only (does not modify files). Use captured groups with $1, $2 or named groups with $name
rg '(?P<name>\w+)@\w+' -r '$name'Replace using a named capture group in the output
rg -o 'pattern' -r 'replacement'Extract and transform matches: print only the replaced text
rg 'old' -r 'new' --passthruShow the entire file with matches replaced in the output (file not modified)

Listing Files and Counting

rg -l 'pattern'List only the filenames of files that contain at least one match
rg --files-without-match 'pattern'List files that contain zero matches
rg --filesList all files that would be searched (without actually searching; useful for debugging ignore rules)
rg -c 'pattern'Count matching lines per file; print filename and count
rg --count-matches 'pattern'Count total individual matches per file (not lines; a line with 3 matches counts as 3)
rg -c --include-zero 'pattern'Count matches per file, including files with zero matches
rg --stats 'pattern'Print aggregate search statistics (files searched, matched, time taken) at the end

Sorting and Depth

rg --sort path 'pattern'Sort results by file path (disables parallel search; adds determinism)
rg --sort modified 'pattern'Sort results by file last-modified time
rg --sortr modified 'pattern'Sort results in descending order by last-modified time (most recent first)
rg -d NUM 'pattern'Limit recursive search to at most NUM directory levels deep
rg --max-filesize 1M 'pattern'Skip files larger than the given size

Piping into fzf

rg 'pattern' | fzfPipe ripgrep results into fzf for interactive fuzzy filtering of matches
rg --no-heading --no-line-number 'pattern' | fzfPipe grep-style output (no grouping, no line numbers) into fzf
rg --files | fzfUse ripgrep to enumerate files (respecting .gitignore) and fuzzy-pick with fzf
fzf --disabled --ansi --bind 'change:reload:rg --color=always --line-number {q} || true' --preview 'bat --color=always {1} --highlight-line {2}' --delimiter :Live interactive search: fzf as a frontend for ripgrep, updating results as you type

Compressed Files and Preprocessing

rg -z 'pattern'Search inside compressed files (gzip, bzip2, xz, lz4, zstd, brotli, lzma); requires decompression binaries in PATH
rg --pre COMMAND 'pattern'Run a preprocessor command on each file before searching its stdout
rg --pre-glob 'GLOB' --pre COMMAND 'pattern'Limit preprocessing to files matching the glob (reduces overhead)

Configuration and Discovery

RIPGREP_CONFIG_PATH=~/.ripgreprc rg 'pattern'Point ripgrep at a config file (one flag per line; # comments supported)
rg --no-config 'pattern'Ignore all config files for this invocation (useful for reproducible one-offs)
rg --debug 'pattern'Print debug info showing which files are skipped and why (invaluable for diagnosing ignore rules)
rg --type-listShow all recognized file types and their associated glob patterns
rg --generate complete-zshGenerate a shell completion script for zsh (also supports bash, fish, powershell)
rg -j NUM 'pattern'Set the number of search threads (default: auto, based on CPU cores)

Killer recipes

Live interactive search with fzf + bat preview

rg --color=always --line-number --no-heading '' | fzf --ansi --disabled --bind 'change:reload:rg --color=always --line-number --no-heading {q} || true' --delimiter : --preview 'bat --color=always --highlight-line {2} {1}' --preview-window 'right:60%:+{2}-5'

A live grep UI: type a query in fzf and ripgrep re-runs instantly, showing bat-rendered source with the matching line highlighted. Assign to an alias (e.g. alias rgf='...') for daily use.

Find all TODOs across a project, sorted and unique

rg -n --sort path 'TODO|FIXME|HACK|WARN' --no-heading | fzf

Collect all code annotations in sorted order across the project; pipe into fzf to jump to any entry interactively.

Extract all unique import paths from TypeScript/JS files

rg -o -t ts -t js "from '([^']+)'" -r '$1' | sort -u

Use ripgrep as a structural extractor: the -o flag and -r replacement emit only the captured group, giving a deduplicated list of every imported module in the project.

Count matches per file, sorted descending (hotspot finder)

rg -c 'pattern' | sort -t: -k2 -rn | head -20

Find the files with the most occurrences of a pattern — useful for spotting which files are doing the most logging, throw the most errors, or have the most TODOs.

Preview a sed-style replacement before writing (safe refactor)

rg 'OldClassName' -r 'NewClassName' --passthru src/ | diff <(rg --passthru 'OldClassName' src/) -

Use ripgrep's --replace with --passthru to preview what a rename would produce across the codebase, diffed against the original, before committing to sed -i or a proper refactor tool.

Search only tracked files (using git ls-files as a source list)

git ls-files -z | xargs -0 rg 'pattern'

Limit the search to files tracked by git, bypassing ripgrep's own ignore logic entirely — useful in repos with unusual .gitignore rules or when you need precisely the working-tree set.

Notes & tips

CONFIG FILE Set RIPGREP_CONFIG_PATH to a file (e.g. ~/.ripgreprc) containing one flag per line. Lines starting with # are comments. Example useful defaults:
--smart-case
--hidden
--glob=!.git/*
--max-columns=150
--max-columns-preview
CLI flags always override config file settings (config is prepended to argv).
IGNORE RULE PRECEDENCE ripgrep reads .gitignore, .git/info/exclude, global gitignore (~/.config/git/ignore), and any .ignore files. Use --debug to see exactly why a file is being skipped.
HIDDEN FLAG GOTCHA -./--hidden includes .git/ itself. Add -g '!.git/**' or --no-ignore-vcs to keep .git out of results when using --hidden.
PERFORMANCE ripgrep uses parallel threads by default. --sort* flags force single-threaded execution. Avoid --sort for large codebases when you only need existence, not order.
PCRE2 FEATURES This build includes PCRE2 with JIT, enabling lookaheads, lookbehinds, and backreferences via the -P flag. The default Rust regex engine does not support these but is faster for simple patterns.
REPLACE IS PREVIEW-ONLY The -r/--replace flag never modifies files. It only transforms ripgrep's stdout. To actually rewrite files, pipe into sd (a modern sed alternative) or use sed -i / perl -pi -e.
NULL SEPARATORS Use --null (-0) to separate filenames with NUL bytes when piping -l output into xargs -0, preventing issues with filenames containing spaces or newlines.
MULTILINE MODE -U/--multiline enables patterns that span line boundaries. Add --multiline-dotall to make . match \n as well. Note: ^ and $ still match at line boundaries in multiline mode.
FZF INTEGRATION Set FZF_DEFAULT_COMMAND='rg --files --hidden --glob !.git' to make fzf use ripgrep for file enumeration globally in your shell, giving you fast, gitignore-aware Ctrl-T completions.