is outdated. I moved my blog over to See you there.

msrp, a useful addition to your toolbox

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.

20 Comments on “msrp, a useful addition to your toolbox”

  1. mzanetti says:

    Seems very useful! I find myself quite often in such a situation. Thanks for the tip!

  2. OlivierM says:

    Does it work for any kind of files under the sun, or just C/CPP ones? I mean, could it also rename python files? How is the whole “file parsing” done? is it modular? is it limited to source files?

  3. ilm says:

    I’d like to think this functionality is part of the refactoring abilities of the IDE.

    Eclipse is great for refactoring code (at least for java), does kdevelop4 support it yet ?

    • Aurélien says:

      Good point. I must confess I am addicted to vim… I don’t know whether kdevelop4 supports this, especially with VCS support.

  4. lefty.crupps says:

    > Note that I escaped the ‘.’ in “foo.”, because
    > msrp supports Perl 5 regular expressions.

    So why not escape the ‘.’ in the immediatly-following ‘bar.’ ? Or why not explain why not to escape that one?

    • Aurélien says:

      Because regular expressions are only used to specify the search pattern. The replacement is not a pattern, so it does not need escaping.

  5. […] From […]

  6. bbigras says:

    I put a Ubuntu package containing your patches on a ppa ->

    Here’s how I did using

    apt-get source msrp

    cd msrp-0.9.4

    mkdir debian/patches
    export QUILT_PATCHES=debian/patches
    touch debian/patches/series

    # tell quilt you want to create a new patch
    quilt new 001_git_support.diff

    # tell quilt which files to track
    quilt add src/ src/ src/cliopt.h

    # edit the files
    patch -p1 < /home/bruno/Téléchargements/msrp-0.9.4-0001-git-support.diff

    # generate the patch
    quilt refresh

    quilt new 002_bzr_support.diff
    quilt add src/ src/ src/cliopt.h
    patch -p1 < /home/bruno/Téléchargements/msrp-0.9.4-0002-bzr-support.diff
    quilt refresh

    # undo local changes (remove quilt patches)
    quilt pop -a

    In the debian/rules file:
    – Add 'include /usr/share/quilt/quilt.make' at the top (below '#!/usr/bin/make -f')
    – Add 'patch' on the same line as 'build-stamp:'
    – Add 'unpatch' on the same line as 'clean:'

    Add 'quilt' on the 'Build-Depends:' line of the debian/control file

    # increate the version thing to '0.9.4-2ubuntu1~ppa0'
    dch -i

    # generate the files to send to my ppa
    debuild -S

    # or build it locally
    dpkg-buildpackage -us -uc

  7. mosra says:

    Great and very useful tool!

    I made a package for ArchLinux:

  8. malex984 says:

    i needed to improve the tool so i forked it at

    What’s new (in the master branch):
    1. can use “\f” in both search and replace expression for the current file name
    2. errors in renaming with git/hg/svn/bzr will be treated correctly and the plain rename used as a fall-back.

    Any contributions and comments are really welcome!

    ps: corresponding package for ArchLinux is at

    • Aurélien says:

      Sounds good! I actually created a repository for it on gitorious and was planning to do a release at some point. On the other hand I always have plenty of stuff on my plate, would you be interested in maintaining msrp?

      • malex984 says:

        oh, i didn’t know about your git repo,
        otherwise i’d try to contribute to it or at least fork from it. sorry!

        as for maintaining msrp: i am also busy with my actual work, but sometimes i may be able to find time to fix bugs/add interesting features, but AFAIK, maintaining means a bit more than that, right?

        So what about a joint maintenance?

        • Aurélien says:

          Not surprising you didn’t know about my git repo as I didn’t announce it anywhere :) no worries about it. Actually I think github is more adapted than gitorious to host projects because it provides a bug tracker, web pages and a few other services.

          To me, maintaining basically means answering questions, reviewing and applying patches, fixing bugs and (most important in my opinion) releasing tarballs from time to time. I assume your repository has all the commits from mine, so I think we can start with your repository. Can you add me as a collaborator to it? oh and we should probably continue this discussion by email :)

  9. […] Motsak, we took over maintenance of msrp, a nice search-and-replace command-line tool. I blogged about this tool some time […]


Get every new post delivered to your Inbox.