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

69 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
name: ripgrep
description: 极速代码搜索工具rg- grep的现代化替代品
category: essential
tool_id: EXT-RG-001
---
# ripgrep (rg) ⭐⭐⭐⭐⭐
## 用途
极速代码搜索,默认递归搜索且遵守.gitignore
## 安装
### macOS
```bash
brew install ripgrep
```
### Ubuntu/Debian
```bash
sudo apt-get install ripgrep
```
## 配置
```bash
# 添加到 ~/.bashrc
alias grep='rg'
```
## 使用
```bash
# 搜索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` -- 只显示包含匹配的文件名
## 技巧
```bash
# 搜索并替换(预览)
rg "old_function" -S | fzf
# 指定编码
rg --encoding utf8 "pattern"
# 与fzf集成
rg "" --files-with-matches | fzf --preview 'rg --color=always "" {}'
```