41

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.

thecoolmacdude
  • 2,036
  • 1
  • 23
  • 38
user45637
  • 411
  • 4
  • 4

6 Answers6

12

It's not possible in Visual Studio at the moment.

You can vote for this feature:

https://developercommunity.visualstudio.com/content/idea/580810/case-preserving-search-replace.html

I usually end up opening Sublime Text, Copy & Paste the code there, perform the case-preserving replacements there and Copy & Paste back to Visual Studio.

thecoolmacdude
  • 2,036
  • 1
  • 23
  • 38
Tom Pažourek
  • 9,582
  • 8
  • 66
  • 107
2

open up the find options when you do the find/replace. check the case-sensitive option. you will have to do the flavours manually unless you do something like: http://www.aaronlerch.com/blog/2007/03/28/visual-studio-find-and-replace-regular-expressions/, or use something like: http://www.download3k.com/MP3-Audio-Video/Utilities-Plug-Ins/Download-Find-Replace.html

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
  • 12
    Doing 4 manual case-sensitive replacements is exactly what I NOT want to do. I am looking for a 'smart' find/replace in VS which does it this automatically (Emacs e.g. does that). – user45637 Dec 15 '08 at 08:46
  • 1
    Upvote the new feature link: https://developercommunity.visualstudio.com/content/idea/580810/case-preserving-search-replace.html – thecoolmacdude Jul 12 '19 at 19:24
2

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

spacetyper
  • 1,471
  • 18
  • 27
0

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)
noelicus
  • 14,468
  • 3
  • 92
  • 111
  • 1
    Upvote the feature request for Visual Studio here: https://developercommunity.visualstudio.com/content/idea/580810/case-preserving-search-replace.html – thecoolmacdude Sep 06 '19 at 18:49
0

Introduced in Visual Studio 2022 17.8 preview as of August 9th, 2023. Demo of the Quick Find and Replace option

[Close up of the option with focus ring3 Option in the real deal Find and Replace

Kevin Scharnhorst
  • 721
  • 1
  • 7
  • 14
-2

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.

JohnFx
  • 34,542
  • 18
  • 104
  • 162
  • This only applies if you are using a .NET language and is not actually a feature if you are using C++. – jheriko Jul 16 '09 at 08:18
  • You can upvote the case preservation feature request for Visual Studio here: https://developercommunity.visualstudio.com/content/idea/580810/case-preserving-search-replace.html – thecoolmacdude Sep 06 '19 at 18:52