Escape the dollar sign in replacements
masterTo include a literal $ character in your replacement string, use $$.
echo "foo" | sd 'foo' '$$bar'
# Output: $barecho "foo" | sd 'foo' '$$bar'repository·master·Indexed 27 days ago
https://github.com/chmln/sdAn intuitive, high-performance find and replace CLI tool that uses JavaScript/Python-style regular expressions. It supports string-literal mode via the -F flag, in-place file modification, and multi-line matching with the -A flag. The tool provides a core Rust library for text replacement and a CLI that supports indexed and named capture groups, regex flags for case-sensitivity and word boundaries, and integration with tools like fd for project-wide replacements.
To include a literal $ character in your replacement string, use $$.
echo "foo" | sd 'foo' '$$bar'
# Output: $barecho "foo" | sd 'foo' '$$bar'To disable regular expression parsing and treat the search and replacement patterns as literal strings, use the -F or --fixed-strings flag. This is useful when your patterns contain many special characters that would otherwise require escaping.
echo 'lots((([]))) of special chars' | sd -F '((([])))' ''By default, sd processes input line-by-line (low memory, streaming). To match patterns across line boundaries (e.g., replacing newlines \n), use the -A or --across flag.
echo -e "hello\nworld" | sd -A '\n' ','-p flag.sd is an intuitive find-and-replace tool. By default, it uses JavaScript and Python-style regular expressions. To perform a simple replacement on standard input:
echo 'search_term' | sd 'find' 'replace'You can install sd using cargo or through various package managers.
cargo install sdsd supports both indexed and named capture groups in the replacement string.
Indexed capture groups use $1, $2, etc.
Named capture groups use $name or ${name}. Using the curly brace syntax ${name} is recommended to avoid ambiguity when the variable is adjacent to other characters.
# Indexed
echo 'cargo +nightly watch' | sd '(\w+)\s+\+(\w+)\s+(\w+)' 'cmd: $1, channel: $2, subcmd: $3'
# Named
echo '123.45' | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '${dollars} dollars and ${cents} cents'To search and replace directly within files, provide the file paths as arguments. Note that sd modifies files in-place by default.
To preview changes without modifying the file, use the -p or --preview flag.
# Replace in a specific file
sd 'window.fetch' 'fetch' http.js
# Preview changes in a file
sd -p 'window.fetch' 'fetch' http.jssd 'window.fetch' 'fetch' http.js
sd -p 'window.fetch' 'fetch' http.jsBecause sd interprets arguments starting with - as flags, you must use the -- delimiter to indicate the end of options if your search or replacement pattern starts with a dash.
echo "./hello foo" | sd "foo" -- "-w"If your search or replacement strings start with a hyphen (-), sd will attempt to interpret them as flags. To signal the end of command options and treat subsequent arguments as positional parameters, use the -- delimiter.
echo "./hello foo" | sd "foo" -- "-w"sd with other tools like fd to perform find and replace across multiple files in a project. Use --exec to run sd on each file found.You can combine sd with other tools like fd to perform project-wide replacements. Use the --exec flag in fd to run sd on every file found.
fd --type file --exec sd 'from "react"' 'from "preact"'