ScriptsApr 14, 2026·3 min read

sd — Intuitive Find & Replace CLI, a Friendlier Alternative to sed

sd replaces sed for most find-and-replace needs with a far simpler syntax. No escaping madness, no BRE/ERE confusion, no differences between GNU and BSD — just sed 99% of us actually wanted.

TL;DR
sd is a Rust CLI that replaces sed for find-and-replace with cleaner syntax, no escaping madness, and preview mode.
§01

What it is

sd is a command-line find-and-replace tool written in Rust that serves as a simpler alternative to sed. It takes two arguments -- pattern and replacement -- and handles the common case of text substitution without sed's cryptic syntax, escaping rules, or BSD vs GNU behavioral differences.

sd targets developers and system administrators who use sed primarily for find-and-replace and want a tool that works the same way on every platform. The regex engine uses Rust's regex crate with Perl-like syntax, supporting non-greedy quantifiers, lookarounds, and named capture groups.

§02

How it saves time or tokens

Sed requires escaping parentheses for capture groups in BRE mode, uses different flags on BSD (macOS) and GNU (Linux), and has a multi-letter command language that most users never need. sd eliminates all of this. The pattern is a regex, the replacement uses $1 $2 for capture groups, and in-place editing is a flag.

The preview flag (-p) shows what would change without modifying files, preventing accidental edits in scripts and CI pipelines.

§03

How to use

  1. Install sd:
# macOS
brew install sd
# Cargo
cargo install sd --locked
  1. Basic find and replace from stdin:
echo 'hello world' | sd world everyone
# hello everyone
  1. In-place file editing with regex:
# Replace version strings using capture groups
sd 'v(\d+)\.(\d+)\.(\d+)' 'version_$1_$2_$3' changelog.md

# Preview before applying
sd -p 'foo' 'bar' *.md
§04

Example

Common sd recipes for development workflows:

# Rename a function across files
sd 'oldFunction' 'newFunction' src/*.go

# Fix import paths
sd 'github.com/old-org/repo' 'github.com/new-org/repo' **/*.go

# Literal string mode (no regex)
sd -s 'https://old-url.com' 'https://new-url.com' config.yaml

# Pipe from find
find . -name '*.json' | xargs sd '"debug": true' '"debug": false'
§05

Related on TokRepo

§06

Common pitfalls

  • sd uses Perl-like regex by default, not POSIX. If you are porting a sed command, capture groups use $1 not \1 in the replacement string.
  • The -s flag switches to literal string matching. Without it, characters like . and * are treated as regex metacharacters, which can cause unexpected matches.
  • sd does not support sed's multi-line addressing, delete commands, or hold space. For complex stream editing beyond find-and-replace, sed or awk is still needed.

Frequently Asked Questions

How does sd differ from sed?+

sd focuses on find-and-replace with a clean two-argument interface. It drops sed's command language, escaping inconsistencies, and BSD vs GNU differences. For the 90% of sed usage that is simple substitution, sd is easier and more consistent.

Does sd support regex capture groups?+

Yes. Use parentheses in the pattern and $1, $2, etc. in the replacement. Named groups are also supported with Rust regex syntax. No escaping of parentheses is needed.

Can sd edit files in place?+

Yes. By default sd modifies files in place when you pass file arguments. Use the -p flag to preview changes without writing, which is recommended before batch operations.

What regex flavor does sd use?+

sd uses Rust's regex crate, which supports Perl-like syntax including non-greedy quantifiers, lookaheads, lookbehinds, and Unicode character classes. It does not support backreferences in patterns.

Is sd faster than sed?+

For simple substitutions, sd and modern sed perform similarly. The primary advantage of sd is usability, not speed. The Rust implementation is efficient, but the real time savings come from not debugging escaping issues.

Citations (3)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets