← 返回命令列表

Linux command

rg 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Search for a pattern

rg "[pattern]"

Search in specific files or directories

rg "[pattern]" [path]

Search case-insensitively

rg -i "[pattern]"

Search for whole words only

rg -w "[pattern]"

Show line numbers

rg -n "[pattern]"

Show context lines

rg -C [3] "[pattern]"

Search specific file types

rg -t [py] "[pattern]"

Search hidden files and directories

rg --hidden "[pattern]"

List files containing matches

rg -l "[pattern]"

Count matches per file

rg -c "[pattern]"

说明

rg (ripgrep) is a fast, recursive grep alternative that respects gitignore rules by default. It searches directories for regex patterns, automatically skipping hidden files, binary files, and ignored paths. Ripgrep uses Rust's regex engine for speed and supports most Perl-compatible regex syntax. It's designed for searching codebases, automatically detecting and skipping binary files and respecting .gitignore patterns. Multiple patterns can be specified with -e. File type filtering uses built-in definitions (use rg --type-list to see available types). Custom globs with -g provide flexible include/exclude patterns. Output is colorized by default when writing to a terminal. Use --color=never for scripts or --color=always for piping to pagers.

参数

-i, --ignore-case
Case-insensitive search
-S, --smart-case
Case-insensitive unless pattern has uppercase
-w, --word-regexp
Match whole words only
-x, --line-regexp
Match whole lines only
-c, --count
Show count of matches per file
-l, --files-with-matches
Show only filenames with matches
-L, --files-without-match
Show only filenames without matches
-n, --line-number
Show line numbers (default when terminal)
-H, --with-filename
Show filename with matches
-A _num_, --after-context
Show num lines after match
-B _num_, --before-context
Show num lines before match
-C _num_, --context
Show num lines before and after match
-t _type_, --type
Search only files of specified type
-T _type_, --type-not
Exclude files of specified type
-g _glob_, --glob
Include/exclude files matching glob
--hidden
Search hidden files and directories
-u, --unrestricted
Reduce filtering (repeatable: -uu, -uuu)
-F, --fixed-strings
Treat pattern as literal string
-e _pattern_
Specify pattern (allows multiple)
-r _replacement_, --replace
Replace matches with text
-o, --only-matching
Show only matched parts

FAQ

What is the rg command used for?

rg (ripgrep) is a fast, recursive grep alternative that respects gitignore rules by default. It searches directories for regex patterns, automatically skipping hidden files, binary files, and ignored paths. Ripgrep uses Rust's regex engine for speed and supports most Perl-compatible regex syntax. It's designed for searching codebases, automatically detecting and skipping binary files and respecting .gitignore patterns. Multiple patterns can be specified with -e. File type filtering uses built-in definitions (use rg --type-list to see available types). Custom globs with -g provide flexible include/exclude patterns. Output is colorized by default when writing to a terminal. Use --color=never for scripts or --color=always for piping to pagers.

How do I run a basic rg 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 rg?

Case-insensitive search