Reuse matched text in the replacement output.
Section: Replacement previews
Use capture groups in replacements
bash
bash
rg --replace '$2_$1' '(\w+)-(\w+)' sample.txtExplanation
Capture-group replacement is great for exploring renames before making edits.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Replacement previews
Preview a simple replacement
Show what matched lines would look like after substitution.
Print replaced fragments only
Combine `-o` and `--replace` to extract transformed matches.
Preview advanced regex replacements
Use PCRE2 pattern features with replacement preview.
Preview and select candidates interactively
Pipe results into a fuzzy finder before editing.
Disable line numbers for transformed output
Print cleaner replacement previews.