Command-line find and replace with a text editor
rep is a command-line utility that takes grep-formatted lines (e.g., from ripgrep) and applies a find and replace on the matches. It does basic find and replace right on the command line, or the grep output can be edited manually with a text editor, and then the changes can be applied.
This means you can use all the features of a text editor including multiple cursors, find and replace, sorting, etc., and then apply the changes. For example:
rg -n "(BananaStand|banana[_-]stand)" > /tmp/out.txt- Edit
/tmp/out.txtin a text editor turning banana stand into lemonade stand rep < /tmp/out.txtto review the changes as a diffrep -w < /tmp/out.txtto write the changes
rep can also perform a find and replace from grep output without using a text editor.
Output a diff to standard output replacing foo with bar:
grep -n foo *` | rep foo bar
Add the -w flag to write the changes to the files in place:
grep -n foo *` | rep foo bar -w
The -n (--line-number) option is required so that grep outputs the line number for each match.
Like wgrep for Emacs, writing to files can also be accomplished by editing the contents of the grep output itself (and omitting the find and replace arguments).
This means for example a workflow like this will work:
grep -n foo * > tmp: Save thegrepmatches to a file namedtmp.sed -i '' s/foo/bar/g tmp: Replacefoowithbarin thetmpfile.rep < tmp: Display a diff of replacingfoowithbarfor eachgrepmatch.rep -w < tmp: Write the changes to replacefoowithbarfor eachgrepmatch.
The flow rep uses when making a change looks like this:
- The input line is broken up into these parts:
<file-path>:<line-number>:[<column-number>:]<line-content>. - The substitution (e.g., the first and second [find and replace] arguments) are applied to the
<line-contents>. - The result is written to the
<file-path>.
These means editing standard input first, and then also applying a find and replace via rep, will also work (e.g., rep bar baz < tmp).
rep is available via cargo:
cargo install rep-grep
The default pager is less, the REP_PAGER environment variable can be used to override the pager (e.g., export REP_PAGER=delta in Bash).
rep -h (or rep --help, --help provides slightly longer explanations of some options) will list help for all the command-line flags.
repwas inspired bywgrepfor Emacs, which allows editinggrepresults in an Emacs buffer and then writing those changes to the source files.- Much of the functionality, and the overall structure of the source code, was borrowed
sd.repbegan as a fork ofsd. - The code for specifying a custom pager for
repwas borrowed fromdelta.

