Replacing
Preview replacements
rg 'foo' -r 'bar'
Input
foo bar
baz
Output
bar bar
baz
Previews replacements without editing. Prints the replaced lines; -r is short for --replace. See the ripgrep guide.
Whole-word matches
rg -w 'foo' -r 'bar'
Input
foo foobar
Output
bar foobar
Matches whole words only. -w limits the replacement to whole-word matches; see ripgrep options.
Rewrite files
rg -l -0 'foo' | xargs -0 sed -i 's/foo/bar/g'
Input
foo
Output
bar
Rewrites matching files in place. -l -0 lists NUL-separated paths, then xargs -0 runs sed -i safely; see the sed manual.
Capture groups
rg -o '(\w+)@(\w+)' -r '$2:$1'
Input
ada@example
Output
example:ada
Reorders capture groups. -o prints only matches and $1/$2 reuse groups; see regex syntax.
Related links
- ripgrep guide (github.com)
- sed manual (gnu.org)
0 Comments for this cheatsheet. Write yours!