Get the FREE Ultimate OpenClaw Setup Guide →

find-replace

npx machina-cli add skill aiskillstore/marketplace/find-replace --openclaw
Files (1)
SKILL.md
2.0 KB

Find Replace

Modern find-and-replace using sd.

sd Basics

# Replace in file (in-place)
sd 'oldText' 'newText' file.txt

# Replace in multiple files
sd 'oldText' 'newText' *.js

# Preview without changing (pipe)
cat file.txt | sd 'old' 'new'

sd vs sed

sedsd
sed 's/old/new/g'sd 'old' 'new'
sed -i 's/old/new/g'sd 'old' 'new' file
sed 's#path/to#new/path#g'sd 'path/to' 'new/path'

Key difference: sd is global by default, no delimiter issues.

Common Patterns

# Variable/function rename
sd 'oldName' 'newName' src/**/*.ts

# Word boundaries (avoid partial matches)
sd '\boldName\b' 'newName' src/**/*.ts

# Import path update
sd "from '../utils'" "from '@/utils'" src/**/*.ts

# Capture groups
sd 'console\.log\((.*)\)' 'logger.info($1)' src/**/*.js

Safe Batch Workflow

# 1. List affected files
rg -l 'oldPattern' src/

# 2. Preview replacements
rg 'oldPattern' -r 'newPattern' src/

# 3. Apply
sd 'oldPattern' 'newPattern' $(rg -l 'oldPattern' src/)

# 4. Verify
rg 'oldPattern' src/  # Should return nothing
git diff              # Review changes

Special Characters

CharacterEscape
.\.
*\*
[ ]\[ \]
$\$
\\\

Tips

TipReason
Always preview with rg -r firstAvoid mistakes
Use git before bulk changesEasy rollback
Use \b for word boundariesAvoid partial matches
Quote patternsPrevent shell interpretation

Additional Resources

For detailed patterns, load:

  • ./references/advanced-patterns.md - Regex, batch workflows, real-world examples

Source

git clone https://github.com/aiskillstore/marketplace/blob/main/skills/0xdarkmatter/find-replace/SKILL.mdView on GitHub

Overview

Find-replace uses sd to perform fast, batch substitutions across files. It improves on sed by defaulting to global replacements and supporting batch patterns. This skill is ideal for refactoring identifiers, updating imports, and other codebase-wide changes.

How This Skill Works

sd runs substitutions from the command line to replace text in a target file or across many files. Use rg to list affected files and preview replacements before applying with sd, following the Safe Batch Workflow. Patterns cover variable/function renames, word boundaries, import path updates, and capture groups.

When to Use It

  • Refactor identifiers (variable/function names) across TS/JS sources
  • Update import paths across a codebase (e.g., from ../utils to @/utils)
  • Batch replacements across multiple files while keeping a safe preview
  • Ensure exact matches by using word boundaries to avoid partial replacements
  • Preview and verify changes with rg and git diff before committing

Quick Start

  1. Step 1: Install sd (brew install sd on macOS or cargo install sd) and confirm sd is on PATH.
  2. Step 2: Run a simple in-file replacement, e.g., sd 'oldText' 'newText' file.txt.
  3. Step 3: Use the Safe Batch Workflow: list affected files with rg, preview with rg, apply with sd, then verify with rg and git diff.

Best Practices

  • Always preview with rg before applying replacements
  • Use word boundaries (where appropriate) to avoid partial matches
  • Keep a backup and use git to track changes
  • Quote patterns to prevent shell interpretation
  • Follow the Safe Batch Workflow: list, preview, apply, verify

Example Use Cases

  • Variable/function rename: sd 'oldName' 'newName' src/**/*.ts
  • Import path update: sd 'from ../utils' 'from @/utils' src/**/*.ts
  • Batch replacement across JS files: sd 'oldPattern' 'newPattern' $(rg -l 'oldPattern' src/)
  • Preview edits with cat: cat file.txt | sd 'old' 'new'
  • Apply and verify with git diff

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers