ripgrep(rg)使用指南
简介
ripgrep(命令名 rg)是一款用 Rust 编写的跨平台命令行搜索工具,用于在目录中递归搜索匹配正则表达式的文本行。
与传统 grep 相比,ripgrep 的主要优势:
- 速度极快:利用 Rust 的并行性和 SIMD 指令,通常比 grep、ag(The Silver Searcher)更快
- 默认遵守
.gitignore:自动跳过.gitignore、.ignore、.rgignore中声明的文件 - 自动跳过隐藏文件和二进制文件
- 支持 Unicode:默认对 Unicode 友好
- 支持 PCRE2:可使用
-P启用 lookahead / lookbehind 等高级正则语法 - 彩色高亮输出:默认高亮匹配内容
安装
macOS(Homebrew)
brew install ripgrep
安装完成后验证:
rg --version
# ripgrep 15.2.0
其他平台
| 平台 | 命令 |
|---|---|
| Ubuntu / Debian | sudo apt install ripgrep |
| Arch Linux | sudo pacman -S ripgrep |
| Windows (Scoop) | scoop install ripgrep |
| Windows (Winget) | winget install BurntSushi.ripgrep.MSVC |
| Cargo(Rust) | cargo install ripgrep |
基本语法
rg [OPTIONS] PATTERN [PATH ...]
PATTERN:正则表达式(默认区分大小写)PATH:文件或目录,省略时默认搜索当前目录
快速上手
# 在当前目录递归搜索 "hello"
rg hello
# 在指定文件中搜索
rg hello src/main.rs
# 在指定目录中搜索
rg hello ./src
常用选项
大小写控制
rg -i hello # 忽略大小写(--ignore-case)
rg -s hello # 强制区分大小写(--case-sensitive)
rg -S hello # 智能大小写:全小写时忽略大小写,含大写时区分(--smart-case)
输出控制
rg -n hello # 显示行号(默认已开启)
rg -N hello # 不显示行号(--no-line-number)
rg -l hello # 只列出匹配文件名(--files-with-matches)
rg -L hello # 只列出不匹配的文件名(--files-without-match)
rg -c hello # 只显示每个文件的匹配行数(--count)
rg --count-matches hello # 显示每个文件的匹配次数(一行多处均计入)
上下文
rg -A 3 hello # 显示匹配行后 3 行(--after-context)
rg -B 3 hello # 显示匹配行前 3 行(--before-context)
rg -C 3 hello # 显示匹配行前后各 3 行(--context)
匹配模式
rg -w hello # 精确匹配整词(--word-regexp)
rg -x hello # 精确匹配整行(--line-regexp)
rg -v hello # 反向匹配:只显示不含 hello 的行(--invert-match)
rg -F 'hello.world' # 把 pattern 当作字面字符串(--fixed-strings,不做正则解析)
rg -U 'foo\nbar' # 多行模式(--multiline)
rg -P '(?<=foo)bar' # 使用 PCRE2 正则(支持 lookahead/lookbehind)
文件过滤
rg hello -g '*.py' # 只搜索 .py 文件(--glob)
rg hello -g '!*.log' # 排除 .log 文件
rg hello --type py # 按文件类型过滤(--type)
rg hello --type-not py # 排除某类型文件
rg hello --hidden # 包含隐藏文件(--hidden)
rg hello --no-ignore # 忽略 .gitignore 等规则
rg hello --no-ignore-vcs # 只忽略 VCS ignore 规则
rg hello -m 5 # 每个文件最多输出 5 个匹配行(--max-count)
查看所有支持的文件类型:
rg --type-list
替换输出
rg hello -r 'HELLO' # 将输出中的 hello 替换为 HELLO(不修改文件,--replace)
仅列出文件
rg --files # 列出所有将被搜索的文件(不做实际搜索)
rg --files --glob '*.md' # 列出所有 .md 文件
实用示例
搜索代码
# 搜索 Python 文件中的 TODO
rg 'TODO' --type py
# 搜索函数定义
rg 'def\s+\w+' --type py
# 搜索 import 语句
rg '^import|^from' --type py
结合管道
# 搜索并统计总匹配行数
rg hello | wc -l
# 搜索结果传给 less 分页
rg hello | less
# 从 stdin 中搜索
cat file.txt | rg hello
排除目录
# 排除 node_modules 目录
rg hello -g '!node_modules/**'
# 排除多个目录
rg hello -g '!{node_modules,dist,build}/**'
搜索多个 pattern
# 同时搜索多个 pattern(OR 关系)
rg -e 'hello' -e 'world'
# 从文件读取 pattern 列表
rg -f patterns.txt
只显示匹配内容(不含文件名 / 行号)
rg -o '\d+' file.txt # 只输出匹配的数字(--only-matching)
rg --no-filename -N hello # 不显示文件名和行号
搜索压缩文件(需配合 --pre)
# 搜索 .gz 文件内容(需 zcat 可用)
rg --pre zcat hello archive.gz
配置文件
ripgrep 支持通过配置文件设置默认选项,避免每次都输入重复参数。
创建配置文件:
mkdir -p ~/.config/ripgrep
touch ~/.config/ripgrep/rc
在 ~/.config/ripgrep/rc 中写入选项(每行一个):
# 默认智能大小写
--smart-case
# 默认包含隐藏文件
--hidden
# 全局排除 .git 目录
--glob=!.git/**
通过环境变量指定配置文件路径:
export RIPGREP_CONFIG_PATH="$HOME/.config/ripgrep/rc"
与 grep / ag 对比
| 特性 | grep | ag | rg |
|---|---|---|---|
| 速度 | 一般 | 快 | 极快 |
| .gitignore 支持 | 无 | 有 | 有 |
| Unicode 支持 | 有限 | 有 | 完整 |
| PCRE2 | 需参数 | 默认 | 需 -P |
| 多行搜索 | 需参数 | 有限 | 有(-U) |
| 配置文件 | 无 | 有 | 有 |
| 维护状态 | 活跃 | 较少更新 | 活跃 |
参考资料
- ripgrep GitHub 主页
- ripgrep User Guide
rg --help查看完整选项说明