sd(1)
A modern, user-friendly replacement for sed that finds and replaces text with regex support and intuitive syntax.
Synopsis
sd [OPTIONS] <PATTERN> <REPLACEMENT> [FILE]...Description
sd is a command-line search and replace utility written in Rust that simplifies text substitution. It uses regex patterns by default and provides intuitive flags that are easier to remember than sed's cryptic options.
When no files are specified, sd reads from standard input. The pattern supports Perl-compatible regular expressions with capture groups, and the replacement string can reference captured groups using $1, $2, etc.
By default, sd only replaces the first match on each line; use -g to replace all occurrences globally.
Common options
| Flag | What it does |
|---|---|
-g, --global | Replace all occurrences on each line, not just the first |
-i, --in-place | Edit files in place, overwriting the original |
-p, --preview | Print what would be replaced without making changes |
-c, --color | Colorize output (on by default when printing to terminal) |
-s, --string-mode | Treat pattern and replacement as literal strings, not regex |
-F, --fixed-strings | Alias for --string-mode |
-n, --no-regex | Disable regex matching |
-e, --pattern | Specify the search pattern (useful when pattern starts with dash) |
Examples
Replace first occurrence of 'foo' with 'bar' on each line, print result to stdout
sd 'foo' 'bar' file.txtReplace all occurrences of 'foo' with 'bar' on each line
sd -g 'foo' 'bar' file.txtRemove trailing whitespace from all lines in file.txt (in-place edit)
sd -i '\s+$' '' file.txtPreview replacing email addresses with 'user at domain' format using capture groups
sd -p '(\w+)@(\w+)' '$1 at $2' emails.txtReplace 'old' with 'new' reading from piped input
cat file.txt | sd 'old' 'new'Treat pattern and replacement as literal strings (ignore special regex characters)
sd -s 'cost: $50' 'cost: $60' invoice.txtCase-insensitive replacement using inline regex flag (?i)
sd '(?i)version' 'VERSION' file.txtReplace all 'TODO' with 'DONE' in all markdown files (in-place)
sd -i -g 'TODO' 'DONE' *.md