Visual Studio中的保留大小写的查找/替换

41

在VisualStudio中似乎没有内置支持大小写敏感的查找/替换功能(请参见相应的feature request)。

我的意思是:搜索'BadJob'并用'GoodJob'替换将执行以下替换

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

我正在寻找一个实现区分大小写的查找/替换宏/插件。如果没有这样的插件,有什么好的起点可以编写自己的插件(最好基于内置的查找/替换功能)。
更新:
我知道可以手动进行4次替换来完成任务,但我正在寻找一种在VS中自动执行的方法(就像Emacs一样)。常见情况:一个名为'foo'的变量和一些函数DoFoo()、GetFoo()等,还有一些包含'foo' 'Foo'等的附加注释。现在将'foo'重命名为'bar',得到变量'bar',函数DoBar(),GetBar()等,只需使用一个查找/替换即可。

1
我也想要这个。通过VS插件实现这个是否容易? - Steven Mark Ford
这是新的链接,用于支持功能请求:https://developercommunity.visualstudio.com/content/idea/580810/case-preserving-search-replace.html - thecoolmacdude
正在实施中!https://devblogs.microsoft.com/visualstudio/keep-your-casing-with-case-preserving-find-and-replace/ - undefined
很高兴看到这个终于得以实施,很多年前就建议过这样的事情(猜想我们所有的老反馈在微软转向新的反馈机制时都丢失了,希望他们现在已经定下了一个永久的反馈机制)。 - undefined
6个回答

12

2
现在可以进行区分大小写的查找和替换,尽管只能针对全大写、全小写或标题大小写(因此无法处理您提供的具体示例)。
详细信息可以在这里找到(如下所示):

保留大小写的查找和替换

在编辑器的查找窗口中进行替换时,现在可以保留大小写。当在编辑器的替换输入框中打开“保留大小写”选项(AB 按钮)时,该功能将被启用。

button

目前 VS Code 仅支持保留全大写全小写标题大小写

example


3
在此处点赞Visual Studio的功能请求:https://developercommunity.visualstudio.com/content/idea/580810/case-preserving-search-replace.html。该功能请求是保留大小写的搜索和替换。 - thecoolmacdude
2
哦,糟糕,我甚至没有注意到问题是关于Visual Studio而不是代码的。 - spacetyper
这太棒了,不知道为什么Visual Studio没有,而VS Code已经有了! - alamoot
我觉得在未来,Visual Studio可能会被逐渐淘汰,被VS Code所取代。 - undefined

2

12
手动进行四次区分大小写的替换不是我想要的。我正在寻找一种在VS中进行智能查找/替换的方法,可以自动完成这个过程(例如Emacs可以做到这一点)。 - user45637
1
点赞新功能链接:https://developercommunity.visualstudio.com/content/idea/580810/case-preserving-search-replace.html - thecoolmacdude

1
在2023年8月9日,Visual Studio 2022中引入了17.8预览版快速查找和替换选项的演示

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


在宣布这个功能的博客文章中,甚至还提到了这个问题。你可以在这篇文章中找到更多相关信息:https://devblogs.microsoft.com/visualstudio/keep-your-casing-with-case-preserving-find-and-replace/ - undefined
可能是因为这个原因,我才提出了这个问题。我想确保所有感兴趣的人都能收到通知。 - undefined

0
这是我处理它的方法:
在Notepad++中打开文件,并运行一个Python脚本,执行一个保留大小写的替换(就像我们以前可以使用Visual Studio宏一样...啊,失去了)。
安装Notepad ++
安装npp python script
创建一个新脚本:
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)

1
在此处为Visual Studio的功能请求点赞:https://developercommunity.visualstudio.com/content/idea/580810/case-preserving-search-replace.html - thecoolmacdude

-2

我知道这并不完全回答了你的问题,但是对于重命名变量和方法名称,你可以通过右键单击标识符并在快捷菜单上使用重命名选项来避免整个问题。这将更新任何对该变量或方法名称的引用。

注意事项:
它仅适用于当前解决方案的范围。
它只会更新托管代码中的引用。
它不会更新字面字符串,如“badcode”。
它不会更新你的注释。

这是我最喜欢的VS2005/2008功能之一。


这仅适用于使用.NET语言的情况,如果您使用C++,则实际上不是一个功能。 - jheriko
您可以在此处为Visual Studio的大小写保留搜索替换功能请求投票:https://developercommunity.visualstudio.com/content/idea/580810/case-preserving-search-replace.html - thecoolmacdude

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接