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

发布于 2024-07-10 12:10:20 字数 650 浏览 6 评论 0 原文

VisualStudio 中似乎没有对保留大小写的查找/替换的内置支持(另请参阅相应的 功能请求)。

我的意思是:搜索“BadJob”并替换为“GoodJob”将执行以下替换

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

所以我正在寻找一个宏/插件来实现保留大小写的查找/替换。 如果不存在,那么我自己编写的一个好的起点是什么(最好基于内置的查找/替换功能)。

更新:
我知道我可以进行 4 个手动替换来完成这项工作,但我正在寻找一种在 VS 中自动完成这项工作的方法(例如 Emacs 就是这样做的)。 常见场景:名为“foo”的变量和一些函数 DoFoo()、GetFoo()...以及一些包含“foo”、“Foo”等的附加注释。 现在将“foo”重命名为“bar”,通过 ONE 查找/替换生成变量“bar”、函数 DoBar()、GetBar() 等。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

故事还在继续 2024-07-17 12:10:20

目前在 Visual Studio 中不可能

您可以为此功能投票:

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

我通常最终会打开 Sublime Text、Copy & 。 将代码粘贴到此处,执行保留大小写的替换,然后复制并复制代码。 粘贴回 Visual Studio。

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.

太阳男子 2024-07-17 12:10:20

现在可以进行保留大小写的查找和替换,尽管仅适用于所有大写、所有小写或标题大小写(因此它不适用于您的特定示例)。

详细信息可以在此处找到(复制如下) :

在查找和替换中保留大小写

现在,您可以在编辑器的“查找”小部件中进行替换时保留大小写。 当编辑器的替换输入框中的“保留大小写”选项(AB 按钮)打开时,该功能就会打开。

按钮

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

示例

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

痴骨ら 2024-07-17 12:10:20

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

[带有对焦环的选项特写3
选项真正的查找和替换

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

人生戏 2024-07-17 12:10:20

执行查找/替换时打开查找选项。 检查区分大小写的选项。 您将必须手动执行口味,除非您执行以下操作: http://www.aaronlerch.com/blog/2007/03/28/visual-studio-find-and-replace-regular-expressions/,或使用类似: http://www.download3k .com/MP3-Audio-Video/Utilities-Plug-Ins/Download-Find-Replace.html

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

夏见 2024-07-17 12:10:20

这就是我处理(out)它的方法:

在 Notepad++ 中打开文件,然后运行一个执行大小写替换的 python 脚本(就像我们以前能够使用 Visual Studio 宏所做的那样......啊,损失)

安装 Notepad++
安装 npp python 脚本
因此创建一个新脚本:

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)

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)
深爱成瘾 2024-07-17 12:10:20

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

注意事项:
它仅适用于当前解决方案的范围。
它仅更新托管代码中的引用。
它不会更新诸如“badcode”之类的文字字符串
它不会更新您的评论。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文