Home

Awesome

sd - search & displace

sd is an intuitive find & replace CLI.

The Pitch

Why use it over any existing tools?

Painless regular expressions.   sd uses regex syntax that you already know from JavaScript and Python. Forget about dealing with quirks of sed or awk - get productive immediately.

String-literal mode.   Non-regex find & replace. No more backslashes or remembering which characters are special and need to be escaped.

Easy to read, easy to write.   Find & replace expressions are split up, which makes them easy to read and write. No more messing with unclosed and escaped slashes.

Smart, common-sense defaults.   Defaults follow common sense and are tailored for typical daily use.

Comparison to sed

While sed does a whole lot more, sd focuses on doing just one thing and doing it well. Here are some cherry-picked examples where sd shines.

Simpler syntax for replacing all occurrences:

Replace newlines with commas:

Extracting stuff out of strings containing slashes:

In place modification of files:

Benchmarks

Simple replacement on ~1.5 gigabytes of JSON

hyperfine --warmup 3 --export-markdown out.md \
  'sed -E "s/\"/'"'"'/g" *.json > /dev/null' \
  'sed    "s/\"/'"'"'/g" *.json > /dev/null' \
  'sd     "\"" "'"'"'"   *.json > /dev/null'
CommandMean [s]Min…Max [s]
sed -E "s/\"/'/g" *.json > /dev/null2.338 ± 0.0082.332…2.358
sed "s/\"/'/g" *.json > /dev/null2.365 ± 0.0092.351…2.378
sd "\"" "'" *.json > /dev/null0.997 ± 0.0060.987…1.007

Result: ~2.35 times faster

Regex replacement on a ~55M json file:

hyperfine --warmup 3 --export-markdown out.md \
  'sed -E "s:(\w+):\1\1:g"    dump.json > /dev/null' \
  'sed    "s:\(\w\+\):\1\1:g" dump.json > /dev/null' \
  'sd     "(\w+)" "$1$1"      dump.json > /dev/null'
CommandMean [s]Min…Max [s]
sed -E "s:(\w+):\1\1:g" dump.json > /dev/null11.315 ± 0.21511.102…11.725
sed "s:\(\w\+\):\1\1:g" dump.json > /dev/null11.239 ± 0.20811.057…11.762
sd "(\w+)" "$1$1" dump.json > /dev/null0.942 ± 0.0040.936…0.951

Result: ~11.93 times faster

Installation

Install through cargo with cargo install sd, or through various package managers

Packaging status

Quick Guide

  1. String-literal mode. By default, expressions are treated as regex. Use -F or --fixed-strings to disable regex.

    > echo 'lots((([]))) of special chars' | sd -F '((([])))' ''
    lots of special chars
    
  2. Basic regex use - let's trim some trailing whitespace

    > echo 'lorem ipsum 23   ' | sd '\s+$' ''
    lorem ipsum 23
    
  3. Capture groups

    Indexed capture groups:

    > echo 'cargo +nightly watch' | sd '(\w+)\s+\+(\w+)\s+(\w+)' 'cmd: $1, channel: $2, subcmd: $3'
    cmd: cargo, channel: nightly, subcmd: watch
    

    Named capture groups:

    > echo "123.45" | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '$dollars dollars and $cents cents'
    123 dollars and 45 cents
    

    In the unlikely case you stumble upon ambiguities, resolve them by using ${var} instead of $var. Here's an example:

    > echo '123.45' | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '$dollars_dollars and $cents_cents'
     and
    
    > echo '123.45' | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '${dollars}_dollars and ${cents}_cents'
    123_dollars and 45_cents
    
  4. Find & replace in a file

    > sd 'window.fetch' 'fetch' http.js
    

    That's it. The file is modified in-place.

    To preview changes:

    > sd -p 'window.fetch' 'fetch' http.js
    
  5. Find & replace across project

    This example uses fd.

    Good ol' unix philosophy to the rescue.

    fd --type file --exec sd 'from "react"' 'from "preact"'
    

    Same, but with backups (consider version control).

    fd --type file --exec cp {} {}.bk \; --exec sd 'from "react"' 'from "preact"'
    

Edge cases

sd will interpret every argument starting with - as a (potentially unknown) flag. The common convention of using -- to signal the end of flags is respected:

$ echo "./hello foo" | sd "foo" "-w"
error: Found argument '-w' which wasn't expected, or isn't valid in this context

USAGE:
    sd [OPTIONS] <find> <replace-with> [files]...

For more information try --help
$ echo "./hello foo" | sd "foo" -- "-w"
./hello -w
$ echo "./hello --foo" | sd -- "--foo" "-w"
./hello -w

Escaping special characters

To escape the $ character, use $$:

❯ echo "foo" | sd 'foo' '$$bar'
$bar