Case preserving find/replace in Visual Studio

Viewed 4818

There seems to be no built-in support for case preserving find/replace in VisualStudio (see also a respective feature request).

What I mean is: searching for 'BadJob' and replacing with 'GoodJob' would do the following replacements

'badjob' -> 'goodjob'  
'BadJob' -> 'GoodJob'  
'badJob' -> 'goodJob'  
'BADJOB' -> 'GOODJOB'

So I am looking for a macro/add-in which implements case preserving find/replace. And if none exists, what is a good starting point to write my own (preferably based on the built-in find/replace capabilities).

Update:
I know I can make 4 manual replacements doing the job, but I am looking for a way to do it automatically in VS (like e.g. Emacs does it). A common scenario: a variable named 'foo' and some functions DoFoo(), GetFoo(), ... and some additional comments containing 'foo' 'Foo' etc. Now rename 'foo' to bar' yielding variable 'bar', functions DoBar(), GetBar() etc. by ONE find/replace.

5 Answers

It is now possible to do case-preserving find and replace, albeit only for all upper case, all lower case, or title case (so it won't work on your specific examples).

Details can be found here (copied below):

Preserve case in Find and Replace

You can now preserve case when doing replacement in the editor's Find widget. The feature is turned on when the Preserve Case option (AB button) is turned on in the editor's Replace input box.

button

Currently VS Code only supports preserve Full Upper Case, Full Lower Case, and Title Case.

example

This is how I've coped with(out) it:

Open the file in Notepad++, and run a python script that does a case-keeping replace (like we used to be able to do with Visual Studio macros ... ah, the loss)

Install Notepad++
Install npp python script
Make a new script thus:

from Npp import *

#Use capitalizeFirst because .capitalize will make the remaining string lower, but in CamelCase examples 
#we will want to preserve the user-typed casing. e.g. YourMonkeyMagic -> MyMonkieMagik 
def capitalizeFirst(str):
    return '%s%s' % (str[:1].upper(), str[1:])

#***  Ask user what to find and replace ***
find_str=notepad.prompt(notepad, 'Find (keeping case)', '')
replace_str=notepad.prompt(notepad, 'Replace (keeping case)', '')

#*** Do a case-sensitive replacement on each type ***
editor.replace(find_str.upper(), replace_str.upper())
editor.replace(find_str.lower(), replace_str.lower())
editor.replace(capitalizeFirst(find_str), capitalizeFirst(replace_str))
editor.replace(find_str, replace_str)

I know this doesn't answer your question exactly as you asked it, but for renaming variables and method names you can avoid the whole problem by right clicking on the identifier and using the rename option on the shortcut menu. That will update any references to that variable or method name.

Caveats:
It only works in the scope of the current solution.
It only updates references in managed code.
It won't update literal strings such as "badcode"
It won't update your comments.

This is one of my favorite features in VS2005/2008.

Related