关于 rg-code-search
rg-code-search 是面向开发者的 ripgrep 搜索指南,涵盖源码和文本文件检索。内容包括模式匹配、筛选、结果格式控制及常见代码审查搜索。
主要功能
- 01正则表达式与多行搜索
- 02按文件类型、glob 和路径筛选
- 03控制上下文与结果输出
- 04搜索定义、测试和代码标记
适用场景
- 01查找函数或类定义
- 02审查代码中的 TODO 或调试语句
- 03将搜索范围限定到指定文件类型
- 04定位匹配项并将文件交给其他工具
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ laurigates/ claude-plugins --skill rg-code-search安装后在 Claude Code 中输入/rg-code-search确认已加载。
详细介绍
rg 代码搜索
使用 rg(ripgrep)作为代码搜索工具的专业知识:它速度极快,并支持强大的筛选和模式匹配。
何时使用此技能
| 在以下情况下使用此技能…… | 在以下情况下改用 fd-file-finding…… |
|---|---|
| 搜索文件内容中的文本或正则表达式模式 | 按名称、扩展名或路径搜索文件 |
按文件类型筛选匹配项(-t py、-t js) | 按修改时间、大小或 -type 筛选文件 |
| 跨源文件进行多行模式匹配 | 查找要传递给其他工具的文件 |
| 在以下情况下使用此技能…… | 在以下情况下改用 binary-analysis…… |
|---|---|
| 搜索源代码或文本编码的文件 | 从编译后的二进制文件或固件中提取字符串 |
| 审计仓库中受跟踪文件里的硬编码模式 | 在 ELF、Mach-O 或 .bin 文件中查找凭据 |
核心能力
ripgrep 的优势
- 速度极快(使用 Rust 编写)
- 自动遵循
.gitignore - 智能大小写不敏感搜索
- 默认递归搜索
- 彩色输出
- 支持多行搜索
- 支持替换
基本用法
简单搜索
# Basic search
rg pattern # Search in current directory
rg "import numpy" # Search for exact phrase
rg function_name # Search for function name
# Case-sensitive search
rg -s Pattern # Force case-sensitive
rg -i PATTERN # Force case-insensitive
按文件类型筛选
# Search specific file types
rg pattern -t py # Python files only
rg pattern -t rs # Rust files only
rg pattern -t js # JavaScript files
rg pattern -t md # Markdown files
# Multiple types
rg pattern -t py -t rs # Python and Rust
# List available types
rg --type-list # Show all known types
按扩展名筛选
# Filter by extension
rg pattern -g '*.rs' # Rust files
rg pattern -g '*.{js,ts}' # JavaScript and TypeScript
rg pattern -g '!*.min.js' # Exclude minified files
高级模式
正则表达式
# Word boundaries
rg '\bfunction\b' # Match whole word "function"
rg '\btest_\w+' # Words starting with test_
# Line anchors
rg '^import' # Lines starting with import
rg 'return$' # Lines ending with return
rg '^class \w+:' # Python class definitions
# Character classes
rg 'TODO|FIXME|XXX' # Find markers
rg '[Ee]rror' # Error or error
rg '\d{3}-\d{4}' # Phone numbers
多行搜索
# Multi-line patterns
rg -U 'fn.*\{.*\}' # Function definitions (Rust)
rg -U 'struct.*{[^}]*}' # Struct definitions
# Context lines
rg -A 5 pattern # Show 5 lines after
rg -B 3 pattern # Show 3 lines before
rg -C 2 pattern # Show 2 lines before and after
输出格式
# Control output
rg -l pattern # List files with matches only
rg -c pattern # Count matches per file
rg --count-matches pattern # Total match count
# Show context
rg -n pattern # Show line numbers (default)
rg -N pattern # Don't show line numbers
rg --heading pattern # Group by file (default)
rg --no-heading pattern # Don't group by file
# Customize output
rg --vimgrep pattern # Vim-compatible format
rg --json pattern # JSON output
高级筛选
路径筛选
# Search in specific directories
rg pattern src/ # Only src/ directory
rg pattern src/ tests/ # Multiple directories
# Exclude paths
rg pattern -g '!target/' # Exclude target/
rg pattern -g '!{dist,build,node_modules}/' # Exclude multiple
# Full path matching
rg pattern -g '**/test/**' # Only test directories
内容筛选
# Search only in files containing pattern
rg --files-with-matches "import.*React" | xargs rg "useState"
# Exclude files by content
rg pattern --type-not markdown
# Search only uncommitted files
rg pattern $(git diff --name-only)
文件大小和隐藏文件
# Include hidden files
rg pattern -u # Include hidden
rg pattern -uu # Include hidden + .gitignore'd
rg pattern -uuu # Unrestricted: everything
# Exclude by size
rg pattern --max-filesize 1M # Skip files over 1MB
代码搜索模式
查找定义
# Function definitions
rg '^def \w+\(' # Python functions
rg 'fn \w+\(' # Rust functions
rg '^function \w+\(' # JavaScript functions
rg '^\s*class \w+' # Class definitions
# Interface/type definitions
rg '^interface \w+' # TypeScript interfaces
rg '^type \w+ =' # Type aliases
rg '^struct \w+' # Struct definitions (Rust/Go)
查找用法
# Find function calls
rg 'functionName\(' # Direct calls
rg '\.methodName\(' # Method calls
# Find imports
rg '^import.*module_name' # Python imports
rg '^use.*crate_name' # Rust uses
rg "^import.*'package'" # JavaScript imports
代码质量检查
# Find TODOs and FIXMEs
rg 'TODO|FIXME|XXX|HACK' # Find all markers
rg -t py '#\s*TODO' # Python TODOs
rg -t rs '//\s*TODO' # Rust TODOs
# Find debug statements
rg 'console\.log' # JavaScript
rg 'println!' # Rust
rg 'print\(' # Python
# Find security issues
rg 'password.*=|api_key.*=' # Potential secrets
rg 'eval\(' # Eval usage
rg 'exec\(' # Exec usage
测试模式
# Find tests
rg '^def test_' -t py # Python tests
rg '#\[test\]' -t rs # Rust tests
rg "describe\(|it\(" -t js # JavaScript tests
# Find skipped tests
rg '@skip|@pytest.mark.skip' -t py
rg '#\[ignore\]' -t rs
rg 'test\.skip|it\.skip' -t js
文件操作
搜索和替换
# Preview replacements
rg pattern --replace replacement
# Perform replacement (requires external tool)
rg pattern -l | xargs sed -i 's/pattern/replacement/g'
# With confirmation (using fd and interactive)
fd -e rs | xargs rg pattern --files-with-matches | xargs -I {} sh -c 'vim -c "%s/pattern/replacement/gc" -c "wq" {}'
与其他工具集成
# Pipe to other commands
rg -l "TODO" | xargs wc -l # Count lines with TODOs
rg "function" --files-with-matches | xargs nvim # Open files in editor
# Combine with fd (prefer fd's native -x execution)
fd -e py -x rg "class.*Test" {} # Find test classes
fd -e rs -x rg "unsafe" {} # Find unsafe blocks
# Count occurrences
rg -c pattern | awk -F: '{sum+=$2} END {print sum}'
统计和分析
# Count total matches
rg pattern --count-matches --no-filename | awk '{sum+=$1} END {print sum}'
# Find most common matches
rg pattern -o | sort | uniq -c | sort -rn
# Files with most matches
rg pattern -c | sort -t: -k2 -rn | head -10
性能优化
提速技巧
# Limit search
rg pattern --max-depth 3 # Limit directory depth
rg pattern -g '*.rs' -t rust # Use type filters
# Parallel processing (default)
rg pattern -j 4 # Use 4 threads
# Memory management
rg pattern --mmap # Use memory maps (faster)
rg pattern --no-mmap # Don't use memory maps
大型代码库策略
# Narrow scope first
rg pattern src/ # Specific directory
rg pattern -t py -g '!test*' # Specific type, exclude tests
# Use file list caching
rg --files > /tmp/files.txt
rg pattern $(cat /tmp/files.txt)
# Exclude large directories
rg pattern -g '!{target,node_modules,dist,build}/'
最佳实践
何时使用 rg
- 搜索代码模式
- 查找函数/类定义
- 代码分析和审计
- 为重构提供支持
- 安全扫描
何时改用 grep
- 需要 POSIX 兼容性
- 简单的一次性搜索
- 管道输入(stdin)
- 系统管理任务
高效搜索技巧
- 对模式中的正则表达式特殊字符进行转义
- 搜索被忽略或隐藏的文件时使用
-u标志 - 使用
--glob '!vendor'排除大型二进制文件或生成文件 - 优先使用 rg 而非 grep,以获得更快速度和更智能的默认设置
快速参考
基本选项
| 选项 | 用途 | 示例 |
|---|---|---|
-t TYPE | 文件类型筛选 | rg -t py pattern |
-g GLOB | Glob 模式 | rg -g '*.rs' pattern |
-i | 大小写不敏感 | rg -i pattern |
-s | 区分大小写 | rg -s Pattern |
-w | 匹配完整单词 | rg -w word |
-l | 列出包含匹配项的文件 | rg -l pattern |
-c | 统计每个文件中的匹配数 | rg -c pattern |
-A N | 匹配行之后的行数 | rg -A 5 pattern |
-B N | 匹配行之前的行数 | rg -B 3 pattern |
-C N | 上下文行数 | rg -C 2 pattern |
-U | 多行匹配 | rg -U 'pattern.*' |
-u | 包含隐藏文件 | rg -u pattern |
--replace | 替换文本 | rg pattern --replace new |
文件类型(常见)
| 类型 | 扩展名 |
|---|---|
-t py | Python(.py、.pyi) |
-t rs | Rust(.rs) |
-t js | JavaScript(.js、.jsx) |
-t ts | TypeScript(.ts、.tsx) |
-t go | Go(.go) |
-t md | Markdown(.md、.markdown) |
-t yaml | YAML(.yaml、.yml) |
-t json | JSON(.json) |
常见命令模式
# Find function definitions across codebase
rg '^\s*(def|fn|function)\s+\w+' -t py -t rs -t js
# Find all imports
rg '^(import|use|require)' -t py -t rs -t js
# Find potential bugs
rg 'TODO|FIXME|XXX|HACK|BUG'
# Find test files and count tests
rg -t py '^def test_' -c
# Find large functions (50+ lines)
rg -U 'def \w+.*\n(.*\n){50,}' -t py
# Security audit
rg 'password|api_key|secret|token' -i -g '!*.{lock,log}'
因此,在开发工作流中,rg 是进行快速、强大代码搜索的首选工具。
评论
支持 Markdown