Linux command
ripgrep 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Search for a pattern recursively in the current directory
rg "[pattern]"
Search only in files of a specific type
rg -t [py] "[pattern]"
Search case-insensitively
rg -i "[pattern]"
Show context lines around matches
rg -C [3] "[pattern]"
List only file names containing matches
rg -l "[pattern]"
Search including hidden files and directories
rg --hidden "[pattern]"
Search for a literal string
rg -F "[literal string]"
Count matches per file
rg -c "[pattern]"
Search with multiline matching
rg -U "[pattern]"
Replace matches with a string
rg "[pattern]" -r "[replacement]"
说明
ripgrep (rg) is a line-oriented search tool that recursively searches directories for regex patterns, optimized for speed through a Rust implementation and parallel directory traversal. It is typically several times faster than traditional grep, ag, or ack for searching large codebases. Smart defaults make it productive out of the box: it automatically respects .gitignore rules, skips binary files and hidden directories, and provides colored output with line numbers. These defaults eliminate the need for complex exclude patterns that are common with recursive grep usage. The --hidden flag includes hidden files, and -u flags progressively disable filters: -u searches ignored files, -uu also searches hidden files, and -uuu also searches binary files. Ripgrep supports full Unicode-aware regex syntax, PCRE2 patterns via -P, literal string matching with -F, and word boundary matching with -w. File type filters (-t) cover dozens of common programming languages and can be customized. Use --type-list to see all available types.
参数
- -i, --ignore-case
- Case-insensitive search.
- -s, --case-sensitive
- Case-sensitive search (overrides -i).
- -S, --smart-case
- Case-insensitive unless pattern contains uppercase.
- -t, --type _TYPE_
- Only search files matching TYPE (e.g., py, js, rust).
- -T, --type-not _TYPE_
- Exclude files matching TYPE.
- -C, --context _NUM_
- Show NUM lines before and after each match.
- -A, --after-context _NUM_
- Show NUM lines after each match.
- -B, --before-context _NUM_
- Show NUM lines before each match.
- -l, --files-with-matches
- Print only file paths containing matches.
- -c, --count
- Print count of matches per file.
- -F, --fixed-strings
- Treat pattern as a literal string, not a regex.
- -w, --word-regexp
- Match whole words only.
- -x, --line-regexp
- Match entire lines only.
- --hidden
- Search hidden files and directories.
- -g, --glob _GLOB_
- Include or exclude files matching glob (prefix with ! to exclude).
- -u, --unrestricted
- Reduce filtering. Repeat (-uu or -uuu) for more.
- -U, --multiline
- Enable multiline matching.
- -P, --pcre2
- Use PCRE2 regex engine for advanced patterns.
- -r, --replace _STRING_
- Replace matches with STRING (supports capture groups).
- -n, --line-number
- Show line numbers (default when connected to terminal).
- -N, --no-line-number
- Suppress line numbers.
- --no-ignore
- Do not respect .gitignore and other ignore files.
- --type-list
- List all built-in file types.
FAQ
What is the ripgrep command used for?
ripgrep (rg) is a line-oriented search tool that recursively searches directories for regex patterns, optimized for speed through a Rust implementation and parallel directory traversal. It is typically several times faster than traditional grep, ag, or ack for searching large codebases. Smart defaults make it productive out of the box: it automatically respects .gitignore rules, skips binary files and hidden directories, and provides colored output with line numbers. These defaults eliminate the need for complex exclude patterns that are common with recursive grep usage. The --hidden flag includes hidden files, and -u flags progressively disable filters: -u searches ignored files, -uu also searches hidden files, and -uuu also searches binary files. Ripgrep supports full Unicode-aware regex syntax, PCRE2 patterns via -P, literal string matching with -F, and word boundary matching with -w. File type filters (-t) cover dozens of common programming languages and can be customized. Use --type-list to see all available types.
How do I run a basic ripgrep example?
Run `rg "[pattern]"` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -i, --ignore-case do in ripgrep?
Case-insensitive search.