replacing-text

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

sd: Fast Find & Replace

sd:快速查找与替换

Always invoke sd skill for find & replace operations - do not execute bash commands directly.
Use sd for intuitive text replacement that's 2-12x faster than sed.
所有查找与替换操作请调用sd技能——不要直接执行bash命令。
使用sd进行直观的文本替换,速度比sed快2-12倍。

When to Use sd vs ripgrep

sd与ripgrep的使用场景对比

Use sd when:
  • Performing replacements on known patterns
  • Batch text transformations across files
  • Need JavaScript-style regex syntax
  • Direct file modifications without search first
Use ripgrep when:
  • Finding unknown patterns or locations
  • Need search results without immediate changes
  • Complex file filtering and type matching
  • Want to preview before modifying
Common workflow: ripgrep skill → sd skill (search first, then replace)
适合使用sd的场景:
  • 对已知模式执行替换操作
  • 跨文件批量文本转换
  • 需要JavaScript风格的正则表达式语法
  • 无需先搜索直接修改文件
适合使用ripgrep的场景:
  • 查找未知模式或位置
  • 仅需要搜索结果而不立即修改
  • 复杂的文件过滤和类型匹配
  • 希望在修改前预览结果
常见工作流:ripgrep技能 → sd技能(先搜索,再替换)

Default Strategy

默认策略

Invoke sd skill for fast find & replace operations. Use when performing replacements on known patterns, batch text transformations across files, or need JavaScript-style regex syntax.
Common workflow: ripgrep skill → sd skill (search first, then replace) or fd skill → sd skill for batch operations.
调用sd技能进行快速查找与替换操作。当你需要对已知模式执行替换、跨文件批量文本转换,或者需要JavaScript风格的正则表达式语法时使用。
常见工作流:ripgrep技能 → sd技能(先搜索,再替换),或fd技能 → sd技能进行批量操作。

Key Options

关键选项

  • -F
    for string-literal mode (no regex)
  • $1, $2
    or
    ${var}
    for capture groups
  • --preview
    to preview changes
  • --
    before patterns starting with
    -
  • $$
    for literal
    $
    in replacement
  • -F
    :字符串字面量模式(不使用正则表达式)
  • $1, $2
    ${var}
    :捕获组
  • --preview
    :预览修改内容
  • --
    :用于以
    -
    开头的模式前
  • $$
    :表示替换内容中的字面量
    $

When to Use

使用场景

  • Quick file edits
  • Pipeline replacements:
    cat file | sd "old" "new"
  • Batch operations across multiple files
  • Complex regex with capture groups
  • 快速文件编辑
  • 管道替换:
    cat file | sd "old" "new"
  • 跨多个文件的批量操作
  • 带捕获组的复杂正则表达式

Common Workflows

常见工作流

  • fd → sd
    : Find files and perform batch replacements
  • ripgrep | sd
    : Search patterns and replace across matches
  • jq | sd
    : Extract JSON data and transform to other formats
  • cat file | sd | bat
    : Transform content and preview with highlighting
  • fd → sd
    :查找文件并执行批量替换
  • ripgrep | sd
    :搜索模式并在匹配内容上执行替换
  • jq | sd
    :提取JSON数据并转换为其他格式
  • cat file | sd | bat
    :转换内容并高亮预览

Core Principle

核心原则

Replace text with familiar JavaScript/Python regex syntax - no sed escape hell.
使用熟悉的JavaScript/Python正则表达式语法替换文本——告别sed的转义噩梦。

Detailed Reference

详细参考

For comprehensive find & replace patterns, regex syntax examples, and workflow tips, load sd guide when needing:
  • Advanced regex patterns with capture groups
  • Pipeline operations and batch processing
  • Escape sequence handling
  • Common text transformation recipes
  • Integration with other command-line tools
The guide includes:
  • String-literal vs regex mode usage
  • Capture group patterns and replacements
  • Pipeline operations and batch processing
  • Advanced regex patterns and edge cases
  • Integration with other command-line tools
当你需要以下内容时,可加载sd指南获取全面的查找与替换模式、正则表达式语法示例和工作流技巧:
  • 带捕获组的高级正则表达式模式
  • 管道操作和批量处理
  • 转义序列处理
  • 常见文本转换方案
  • 与其他命令行工具的集成
该指南包含:
  • 字符串字面量与正则表达式模式的使用
  • 捕获组模式与替换
  • 管道操作和批量处理
  • 高级正则表达式模式与边缘情况
  • 与其他命令行工具的集成

Skill Combinations

技能组合

For Discovery Phase

发现阶段

  • ripgrep → sd: Search patterns first, then replace across matches
  • fd → sd: Find files and perform batch replacements
  • jq/yq → sd: Extract data and transform to other formats
  • ripgrep → sd:先搜索模式,再在匹配内容上执行替换
  • fd → sd:查找文件并执行批量替换
  • jq/yq → sd:提取数据并转换为其他格式

For Analysis Phase

分析阶段

  • sd --preview → bat: Preview changes with syntax highlighting
  • sd → fzf: Interactive selection of changes to apply
  • sd → jq/yq: Transform data back to structured formats
  • sd --preview → bat:通过语法高亮预览修改内容
  • sd → fzf:交互式选择要应用的修改
  • sd → jq/yq:将转换后的数据转回结构化格式

For Refactoring Phase

重构阶段

  • sd → searching-text: Verify replacements didn't introduce issues
  • sd → analyzing-code-structure: Follow up with structural changes if needed
  • sd → analyzing-code: Measure impact of text-based changes
  • sd → searching-text:验证替换操作未引入问题
  • sd → analyzing-code-structure:如有需要,后续进行结构调整
  • sd → analyzing-code:评估基于文本的修改带来的影响

Integration Examples

集成示例

bash
undefined
bash
undefined

Find and replace across project

跨项目查找并替换

fd -e js -x sd "oldPattern" "newPattern" --preview
fd -e js -x sd "oldPattern" "newPattern" --preview

Chain transformations

链式转换

cat config.json | jq '.version' | sd '"v' '' | sd '"' '' | xargs git tag
cat config.json | jq '.version' | sd '"v' '' | sd '"' '' | xargs git tag

Interactive replacement with preview

带预览的交互式替换

rg "deprecated" -l | fzf --multi | xargs sd "deprecated" "legacy" --preview | bat
undefined
rg "deprecated" -l | fzf --multi | xargs sd "deprecated" "legacy" --preview | bat
undefined