返回 Skill 列表
extension
分类: 开发与工程无需 API Key

rg

当用户要求“在代码库中搜索模式”、“查找函数的所有使用情况”、“跨文件搜索字符串”、“查找TODO注释”、“在特定文件类型中搜索”、“计数出现次数”,或者需要带有上下文、行号或特殊标志的高效文本搜索时,应使用此技能。

person作者: jakexiaohubgithub

rg: Ripgrep Text Search

Efficient text search using ripgrep with one-shot patterns that minimize iterations.

Tool Selection

Grep tool (built-in) - Use for structured searches:

  • Basic pattern matching with structured output
  • File type filtering with type parameter
  • Handles 95% of search needs

Bash(rg) - Use when needing:

  • Fixed string search (-F)
  • Invert match (-v)
  • Word boundaries (-w)
  • Context lines (-C 2)
  • Pipe composition (| head, | wc -l)
  • One-shot results with line numbers

Quick Reference

# Basic search with line numbers and context
rg -n -C 2 'pattern' .

# Case insensitive
rg -i 'pattern' .

# Fixed string (no regex)
rg -F 'console.log(' .

# Word boundaries
rg -w 'function' .

# Specific file types
rg -t js 'import' .
rg -t py 'def ' .
rg -t ts 'interface' .

# Exclude directories
rg --glob '!node_modules' 'pattern' .

# Count matches
rg -c 'TODO' .

# Files only (no content)
rg -l 'pattern' .

Common Patterns

# Find function definitions
rg 'function \w+\(' -t js .
rg 'def \w+\(' -t py .
rg 'func \w+\(' -t go .

# Find imports/requires
rg "import .* from" -t js .
rg "require\(" -t js .

# Find TODOs/FIXMEs
rg 'TODO|FIXME' .

# Find console.log (for cleanup)
rg -F 'console.log' -t js .

# Find class definitions
rg 'class \w+' -t ts .

# Find API endpoints
rg "app\.(get|post|put|delete)\(" -t js .

File Type Flags

-t js      # JavaScript
-t ts      # TypeScript
-t py      # Python
-t go      # Go
-t rust    # Rust
-t ruby    # Ruby
-t java    # Java
-t cpp     # C++
-t md      # Markdown
-t json    # JSON
-t yaml    # YAML

Context and Output

# Lines before/after match
rg -B 3 'pattern' .     # 3 lines before
rg -A 3 'pattern' .     # 3 lines after
rg -C 3 'pattern' .     # 3 lines both

# Show line numbers
rg -n 'pattern' .

# Show column numbers
rg --column 'pattern' .

# JSON output
rg --json 'pattern' .

Pipe Composition

# First 10 matches
rg 'pattern' . | head -10

# Count total matches
rg 'pattern' . | wc -l

# Sort by file
rg 'pattern' . | sort

# Unique files only
rg -l 'pattern' . | sort -u

Core Principle

Get files, line numbers, and context in a single call. Minimize search iterations.