content-search

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Content Search Skill

内容搜索Skill

Search file contents using regular expressions with ripgrep (rg) or grep fallback. Optimized for searching code and text files.
使用ripgrep(rg)或备用的grep,通过正则表达式搜索文件内容。专为搜索代码和文本文件优化。

When to Use

适用场景

USE this skill when:
  • Finding where a function is defined
  • Searching for specific patterns in code
  • Locating files containing specific text
  • Analyzing codebase structure
DON'T use this skill when:
  • Binary file search (use file command)
  • Simple filename matching (use find or glob)
  • Regex in filenames only (use find -regex)
当以下情况时使用该Skill:
  • 查找函数定义的位置
  • 在代码中搜索特定模式
  • 定位包含特定文本的文件
  • 分析代码库结构
以下情况请勿使用该Skill:
  • 二进制文件搜索(使用file命令)
  • 简单文件名匹配(使用find或glob)
  • 仅在文件名中使用正则表达式(使用find -regex)

Features

功能特性

  • Ripgrep (rg) with grep fallback
  • Multiple output modes: content, files, count
  • Glob-based file filtering
  • Context lines before/after matches
  • Case-sensitive/insensitive search
  • Multiline pattern support (rg only)
  • Respects .gitignore
  • 基于ripgrep(rg)并支持grep备用方案
  • 多种输出模式:内容、文件、计数
  • 基于glob的文件过滤
  • 匹配结果前后的上下文行
  • 大小写敏感/不敏感搜索
  • 多行模式支持(仅rg)
  • 遵循.gitignore规则

Usage

使用方法

Basic Search

基础搜索

bash
node /job/.pi/skills/content-search/search.js --pattern "console.log"
bash
node /job/.pi/skills/content-search/search.js --pattern "console.log"

Search with File Filter

带文件过滤的搜索

bash
node /job/.pi/skills/content-search/search.js \
  --pattern "function.*init" \
  --include "*.js" \
  --include "*.ts"
bash
node /job/.pi/skills/content-search/search.js \
  --pattern "function.*init" \
  --include "*.js" \
  --include "*.ts"

Files with Matches Only

仅显示包含匹配项的文件

bash
node /job/.pi/skills/content-search/search.js \
  --pattern "TODO" \
  --output-mode files_with_matches
bash
node /job/.pi/skills/content-search/search.js \
  --pattern "TODO" \
  --output-mode files_with_matches

Match Count Per File

每个文件的匹配计数

bash
node /job/.pi/skills/content-search/search.js \
  --pattern "error" \
  --output-mode count
bash
node /job/.pi/skills/content-search/search.js \
  --pattern "error" \
  --output-mode count

With Context

包含上下文

bash
node /job/.pi/skills/content-search/search.js \
  --pattern "async function" \
  --context-before 2 \
  --context-after 2
bash
node /job/.pi/skills/content-search/search.js \
  --pattern "async function" \
  --context-before 2 \
  --context-after 2

Case Insensitive

大小写不敏感

bash
node /job/.pi/skills/content-search/search.js \
  --pattern "todo" \
  --case-insensitive
bash
node /job/.pi/skills/content-search/search.js \
  --pattern "todo" \
  --case-insensitive

CLI Options

CLI选项

--pattern <regex>        Pattern to search for (required)
--path <directory>       Directory to search (default: .)
--include <glob>         File glob filter (e.g., "*.rs")
--exclude <glob>         Exclude glob pattern
--output-mode <mode>     content|files_with_matches|count
--case-insensitive       Case-insensitive search
--context-before <n>     Lines before match
--context-after <n>      Lines after match
--multiline              Enable multiline (rg only)
--max-results <n>        Maximum results (default: 1000)
--pattern <regex>        要搜索的模式(必填)
--path <directory>       要搜索的目录(默认:当前目录)
--include <glob>         文件glob过滤器(例如:"*.rs")
--exclude <glob>         排除的glob模式
--output-mode <mode>     content|files_with_matches|count
--case-insensitive       大小写不敏感搜索
--context-before <n>     匹配结果前的行数
--context-after <n>      匹配结果后的行数
--multiline              启用多行模式(仅rg)
--max-results <n>        最大结果数(默认:1000)

Output Format

输出格式

Content mode:
json
{
  "matches": [
    {
      "file": "src/index.js",
      "line": 42,
      "content": "  console.log('Hello');",
      "before": [],
      "after": []
    }
  ],
  "totalMatches": 1
}
Files mode:
json
{
  "files": ["src/index.js", "src/utils.js"],
  "totalFiles": 2
}
Count mode:
json
{
  "counts": [
    {"file": "src/index.js", "count": 5},
    {"file": "src/utils.js", "count": 3}
  ],
  "totalMatches": 8
}
内容模式:
json
{
  "matches": [
    {
      "file": "src/index.js",
      "line": 42,
      "content": "  console.log('Hello');",
      "before": [],
      "after": []
    }
  ],
  "totalMatches": 1
}
文件模式:
json
{
  "files": ["src/index.js", "src/utils.js"],
  "totalFiles": 2
}
计数模式:
json
{
  "counts": [
    {"file": "src/index.js", "count": 5},
    {"file": "src/utils.js", "count": 3}
  ],
  "totalMatches": 8
}

Example Patterns

示例模式

Find all function definitions:
bash
node /job/.pi/skills/content-search/search.js \
  --pattern "fn\s+\w+\s*\(" \
  --include "*.rs"
Find TODOs:
bash
node /job/.pi/skills/content-search/search.js \
  --pattern "TODO|FIXME|XXX" \
  --case-insensitive
Find imports:
bash
node /job/.pi/skills/content-search/search.js \
  --pattern "^import.*from.*$" \
  --include "*.js" --include "*.ts"
Find error handling:
bash
node /job/.pi/skills/content-search/search.js \
  --pattern "try\s*{|catch\s*\(|\.catch\(" \
  --include "*.js"
查找所有函数定义:
bash
node /job/.pi/skills/content-search/search.js \
  --pattern "fn\s+\w+\s*\(" \
  --include "*.rs"
查找TODO项:
bash
node /job/.pi/skills/content-search/search.js \
  --pattern "TODO|FIXME|XXX" \
  --case-insensitive
查找导入语句:
bash
node /job/.pi/skills/content-search/search.js \
  --pattern "^import.*from.*$" \
  --include "*.js" --include "*.ts"
查找错误处理代码:
bash
node /job/.pi/skills/content-search/search.js \
  --pattern "try\s*{|catch\s*\(|\.catch\(" \
  --include "*.js"

Performance Tips

性能提示

  • Use specific
    --include
    patterns to reduce search scope
  • Prefer ripgrep (rg) over grep - it's 10-100x faster
  • Limit results with
    --max-results
  • Use
    --output-mode files_with_matches
    for quick existence checks
  • Exclude node_modules:
    --exclude "node_modules/*"
  • 使用特定的
    --include
    模式缩小搜索范围
  • 优先使用ripgrep(rg)而非grep——它的速度快10-100倍
  • 使用
    --max-results
    限制结果数量
  • 对于快速存在性检查,使用
    --output-mode files_with_matches
  • 排除node_modules:
    --exclude "node_modules/*"