Case preserving find/replace in Visual Studio

2020-02-23 05:25发布

问题:

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.

回答1:

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.



回答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



回答3:

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.

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



回答4:

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)


回答5:

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.