sd

repository·master·Indexed 27 days ago

https://github.com/chmln/sd

An 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.

Tokens
2.7K
Snippets
10
Records
31
Agent score
92%

What's inside sd

  1. Use fixed-string mode with -F

    master

    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 '((([])))' ''
  2. Use across mode with -A

    master

    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' ','
  3. Use sd for basic search and replace

    master

    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'
  4. Use capture groups in replacements

    master

    sd 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'
  5. Modify files in-place

    master

    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.js
    sd 'window.fetch' 'fetch' http.js
    sd -p 'window.fetch' 'fetch' http.js
  6. Handle arguments starting with dashes using --

    master

    Because 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"
  7. Handle flags starting with hyphens using --

    master

    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"
  8. Search and replace across multiple files using fd

    master

    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"'