Ripgrep技能使用说明
2026-03-30
新闻来源:网淘吧
围观:10
电脑广告
手机广告
ripgrep (rg)
快速、智能的递归搜索。默认会遵守.gitignore规则。
快速入门
基础搜索
# Search for "TODO" in current directory
rg "TODO"
# Case-insensitive search
rg -i "fixme"
# Search specific file types
rg "error" -t py # Python files only
rg "function" -t js # JavaScript files
常见模式
# Whole word match
rg -w "test"
# Show only filenames
rg -l "pattern"
# Show with context (3 lines before/after)
rg -C 3 "function"
# Count matches
rg -c "import"
高级用法
文件类型过滤
# Multiple file types
rg "error" -t py -t js
# Exclude file types
rg "TODO" -T md -T txt
# List available types
rg --type-list
搜索修饰符
# Regex search
rg "user_\d+"
# Fixed string (no regex)
rg -F "function()"
# Multiline search
rg -U "start.*end"
# Only show matches, not lines
rg -o "https?://[^\s]+"
路径过滤
# Search specific directory
rg "pattern" src/
# Glob patterns
rg "error" -g "*.log"
rg "test" -g "!*.min.js"
# Include hidden files
rg "secret" --hidden
# Search all files (ignore .gitignore)
rg "pattern" --no-ignore
替换操作
# Preview replacements
rg "old_name" --replace "new_name"
# Actually replace (requires extra tool like sd)
rg "old_name" -l | xargs sed -i 's/old_name/new_name/g'
性能提示
# Parallel search (auto by default)
rg "pattern" -j 8
# Skip large files
rg "pattern" --max-filesize 10M
# Memory map files
rg "pattern" --mmap
常见用例
在代码中查找待办事项:

rg "TODO|FIXME|HACK" --type-add 'code:*.{rs,go,py,js,ts}' -t code
在特定分支中搜索:
git show branch:file | rg "pattern"
查找包含多个模式的文件:
rg "pattern1" | rg "pattern2"
带上下文和颜色的搜索:
rg -C 2 --color always "error" | less -R
与 grep 的对比
- 更快:通常比 grep 快 5-10 倍
- 更智能:遵守
.gitignore跳过二进制文件 - 更好的默认设置:递归搜索、彩色输出、显示行号
- 更简便:常用任务的语法更简洁
使用技巧
rg通常比grep -r- 更快
使用
-t进行文件类型过滤,而非--include - 与其他工具结合使用:
rg 模式 -l | xargs 工具 - 在
~/.ripgreprc - 中添加自定义类型
使用
--stats查看搜索性能
文档
GitHub:https://github.com/BurntSushi/ripgrep用户指南:https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md
文章底部电脑广告
手机广告位-内容正文底部


微信扫一扫,打赏作者吧~