Linux command
sd 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Replace text
sd '[find]' '[replace]' [path/to/file]
Replace text using literal strings
sd -F '[find]' '[replace]' [path/to/file]
Preview changes
sd -p '[find]' '[replace]' [path/to/file]
Replace text from stdin
echo '[text]' | sd '[find]' '[replace]'
Use capture groups
sd '(\w+)@(\w+)' '$1 at $2' [file]
Replace in multiple files
fd --type file --exec sd '[find]' '[replace]'
Replace text containing special characters
sd -F '$.value' 'newValue' [config.json]
说明
sd is a fast, intuitive find-and-replace command-line tool written in Rust. It serves as a modern alternative to sed, focusing specifically on text substitution with a simpler, more readable syntax. Unlike sed, sd uses JavaScript/Python-style regular expressions that most developers already know. The find and replace patterns are provided as separate arguments rather than combined in a complex expression, making commands easier to write and understand. Capture groups work intuitively: use $1, $2 for indexed groups, or $name for named groups defined with (?P\<name\>pattern). To include a literal dollar sign in the replacement, escape it as $$. When no files are specified, sd reads from standard input. Combined with tools like fd, it enables powerful batch replacements across entire codebases.
参数
- -F, --fixed-strings
- Treat find and replace patterns as literal strings, not regular expressions
- -p, --preview
- Preview changes without modifying files
- -f, --flags _FLAGS_
- Regex flags: c (case-sensitive), i (case-insensitive), m (multiline), s (dotall)
- --
- End of flags; allows patterns starting with a dash
FAQ
What is the sd command used for?
sd is a fast, intuitive find-and-replace command-line tool written in Rust. It serves as a modern alternative to sed, focusing specifically on text substitution with a simpler, more readable syntax. Unlike sed, sd uses JavaScript/Python-style regular expressions that most developers already know. The find and replace patterns are provided as separate arguments rather than combined in a complex expression, making commands easier to write and understand. Capture groups work intuitively: use $1, $2 for indexed groups, or $name for named groups defined with (?P\<name\>pattern). To include a literal dollar sign in the replacement, escape it as $$. When no files are specified, sd reads from standard input. Combined with tools like fd, it enables powerful batch replacements across entire codebases.
How do I run a basic sd example?
Run `sd '[find]' '[replace]' [path/to/file]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -F, --fixed-strings do in sd?
Treat find and replace patterns as literal strings, not regular expressions