ripgrep (rg)
Fast, smart recursive search. Respects .gitignore by default.
Quick Start
Basic search
CODEBLOCK0
Common patterns
CODEBLOCK1
Advanced Usage
File type filtering
CODEBLOCK2
Search modifiers
CODEBLOCK3
Path filtering
CODEBLOCK4
Replacement Operations
CODEBLOCK5
Performance Tips
CODEBLOCK6
Common Use Cases
Find TODOs in code:
CODEBLOCK7
Search in specific branches:
CODEBLOCK8
Find files containing multiple patterns:
CODEBLOCK9
Search with context and color:
CODEBLOCK10
Comparison to grep
- - Faster: Typically 5-10x faster than grep
- Smarter: Respects
.gitignore, skips binary files - Better defaults: Recursive, colored output, line numbers
- Easier: Simpler syntax for common tasks
Tips
- -
rg is often faster than INLINECODE3 - Use
-t for file type filtering instead of INLINECODE5 - Combine with other tools: INLINECODE6
- Add custom types in INLINECODE7
- Use
--stats to see search performance
Documentation
GitHub: https://github.com/BurntSushi/ripgrep
User Guide: https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md
ripgrep (rg)
快速、智能的递归搜索。默认遵循 .gitignore 规则。
快速入门
基础搜索
bash
在当前目录搜索 TODO
rg TODO
不区分大小写搜索
rg -i fixme
搜索特定文件类型
rg error -t py # 仅 Python 文件
rg function -t js # 仅 JavaScript 文件
常见模式
bash
全词匹配
rg -w test
仅显示文件名
rg -l pattern
显示上下文(前后各3行)
rg -C 3 function
统计匹配次数
rg -c import
高级用法
文件类型过滤
bash
多种文件类型
rg error -t py -t js
排除文件类型
rg TODO -T md -T txt
列出可用类型
rg --type-list
搜索修饰符
bash
正则搜索
rg user_\d+
固定字符串(非正则)
rg -F function()
多行搜索
rg -U start.*end
仅显示匹配内容,不显示整行
rg -o https?://[^\s]+
路径过滤
bash
搜索特定目录
rg pattern src/
通配符模式
rg error -g *.log
rg test -g !*.min.js
包含隐藏文件
rg secret --hidden
搜索所有文件(忽略 .gitignore)
rg pattern --no-ignore
替换操作
bash
预览替换结果
rg old
name --replace newname
实际替换(需要额外工具如 sd)
rg old
name -l | xargs sed -i s/oldname/new_name/g
性能优化技巧
bash
并行搜索(默认自动启用)
rg pattern -j 8
跳过大文件
rg pattern --max-filesize 10M
内存映射文件
rg pattern --mmap
常见用例
在代码中查找 TODO:
bash
rg TODO|FIXME|HACK --type-add code:*.{rs,go,py,js,ts} -t code
在特定分支中搜索:
bash
git show branch:file | rg pattern
查找包含多个模式的文件:
bash
rg pattern1 | rg pattern2
带上下文和颜色的搜索:
bash
rg -C 2 --color always error | less -R
与 grep 对比
- - 更快: 通常比 grep 快 5-10 倍
- 更智能: 遵循 .gitignore,跳过二进制文件
- 更好的默认设置: 递归搜索、彩色输出、显示行号
- 更易用: 常见任务的语法更简洁
使用技巧
- - rg 通常比 grep -r 更快
- 使用 -t 进行文件类型过滤,而非 --include
- 与其他工具组合使用:rg pattern -l | xargs tool
- 在 ~/.ripgreprc 中添加自定义类型
- 使用 --stats 查看搜索性能
文档
GitHub:https://github.com/BurntSushi/ripgrep
用户指南:https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md