Aurélien Gâteau

msrp, a useful addition to your toolbox

written on Wednesday, March 10, 2010

Have you ever faced the need to rename a file and do a search and replace to adjust all places where it is referenced? For example if you are a C++ developer you may want to rename foo.h and foo.cpp to bar.h and bar.cpp, but you must also replace all lines which says #include "foo.h" with #include "bar.h" in other source files... and don't forget updating the build system files as well!

This is not really difficult, but it's tedious and error-prone.

Enter msrp, Meta Search and Replace Program, a handy command-line utility designed to do search and replaces in both file contents and file names. In my example, renaming foo.h and foo.cpp to bar.h and bar.cpp can be done with this single command:

msrp 'foo\.' bar. src/dir/

Note that I escaped the '.' in "foo.", because msrp supports Perl 5 regular expressions.

What makes msrp even more useful is that it is VCS aware: If I replace the previous command with:

msrp --svn 'foo\.' bar. src/dir/

Then msrp will rename my files using "svn move" instead of doing a plain rename!

Unfortunately, it is unmaintained right now: his developer does not have time anymore to work on it. You can however install it via apt-get on Debian and Ubuntu, or download the source tarball on the page of the Debian package (the developer was also a Debian packager (Hint: if you are a Debian packager, there is an orphaned package waiting for you!))

Msrp comes with support for Mercurial and Subversion by default, but I wrote patches to add support for Git and Bazaar. Note that these patches are not in the 0.9.4 version which is shipped by Debian and Ubuntu.

Give it a try!

Update: added missing quotes around foo\. to prevent the shell from interpreting the backslash itself.

This post was tagged command line, msrp, replace, search, tips and tool