Possible Duplicate:
Case preserving substitute in Vim
Is it possible to do a search and replace in vim that preserves the case of the search term? This was a useful feature in intelliJ that I miss.
For instance, something like:
:s/[uU]ser/[pP]erson/ (obviously, this doesn't work)
Such that:
user->person
User->Person
Another example with multiple characters to preserve:
:s/[mM]y[uU]ser/[tT]his[pP]erson/g
Such that:
myuser->thisperson
myUser->thisPerson
MyUser->ThisPerson
Redone the answer after giving it some thought ;-)
Of course it can be improved, but the idea stays.
There are a few approaches that can be taken. If you want to stick with basic Vim functionality, you can do something like
If you have Vim built with Perl bindings, you can make use of
:perldo
. Depending on the length of the matching/replacing words and where the case you want to preserve is, this may or may not work.Or you can make use of one of the various scripts that implement such functionality.
There's a plugin for vim: keepcase.vim