Files
gh-dwsy-ai-runtime-ai-runti…/skills/toolkit/docs/tools/external/ripgrep.md
2025-11-29 18:24:37 +08:00

1.1 KiB
Raw Blame History

name, description, category, tool_id
name description category tool_id
ripgrep 极速代码搜索工具rg- grep的现代化替代品 essential EXT-RG-001

ripgrep (rg)

用途

极速代码搜索,默认递归搜索且遵守.gitignore

安装

macOS

brew install ripgrep

Ubuntu/Debian

sudo apt-get install ripgrep

配置

# 添加到 ~/.bashrc
alias grep='rg'

使用

# 搜索Python文件中的TODO
rg "TODO" -g "*.py"

# 显示上下文3行
rg -A 3 -B 3 "def function_name" app.py

# 统计匹配数
rg --count "import"

# 搜索并打开文件
rg "TODO" --files-with-matches | fzf | xargs bat

常用选项

  • -i -- 忽略大小写
  • -g -- 文件模式匹配glob
  • -A NUM -- 显示匹配后NUM行
  • -B NUM -- 显示匹配前NUM行
  • -C NUM -- 显示匹配前后各NUM行
  • --count -- 统计匹配数
  • --files-with-matches -- 只显示包含匹配的文件名

技巧

# 搜索并替换(预览)
rg "old_function" -S | fzf

# 指定编码
rg --encoding utf8 "pattern"

# 与fzf集成
rg "" --files-with-matches | fzf --preview 'rg --color=always "" {}'