visual-studio VisualStudio:重命名变量时的快捷方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1098654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:44:22  来源:igfitidea点击:

VisualStudio: Shortcut for when Renaming Variable

visual-studiokeyboard-shortcuts

提问by InfoStatus

In Visual Studio, when you rename a variable name in its definition, there's a little underline in the last letter so if you hover that, you'll get the option "Rename OldVaribleName to NewVariableName" and so rename all entries in that procedure of that variable.

在 Visual Studio 中,当您在其定义中重命名变量名称时,最后一个字母中有一个小下划线,因此如果将其悬停,您将获得“将 OldVaribleName 重命名为 NewVariableName”选项,因此重命名该过程中的所有条目多变的。

Is there a shortcut for that and not use the mouse?

有没有捷径而不使用鼠标?

回答by Ahmad Mageed

These are known as Smart Tags.

这些被称为智能标签。

You can use CTRL+.

您可以使用CTRL+.

Shift+Alt+F10is another option.

Shift+ Alt+F10是另一种选择。

回答by joshmcode

By way of an updated answer...

通过更新的答案...

Update for Visual Studio 2019

Visual Studio 2019 更新

Same behavior as 2017.

与 2017 年相同的行为。

Update for Visual Studio 2017

Visual Studio 2017 更新

In Visual Studio 2017 click the variable and then press Ctrl + r, r. This works without an issue (see comments below about differences in Visual Studio 2015 where the whole command had to be repeated).

在 Visual Studio 2017 中,单击变量,然后按Ctrl + r, r。这没有问题(请参阅下面关于 Visual Studio 2015 中必须重复整个命令的差异的评论)。

Visual Studio 2015

视觉工作室 2015

In Visual Studio 2015, if you click on the variable and use Ctrl + r, Ctrl + rit will lock onto the variable name and highlight all instances of the variable. Then as you begin typing the new variable name it will rename all instances at once. Hit enter to escape, once you are finished (note: differences might exist between update versions. See comments below).

在 Visual Studio 2015 中,如果您单击变量并使用Ctrl + r, Ctrl + r它,它将锁定变量名称并突出显示该变量的所有实例。然后,当您开始键入新变量名称时,它将立即重命名所有实例。完成后按回车键退出(注意:更新版本之间可能存在差异。请参阅下面的评论)。

Edit: One warning on this approach. If you are using Razor in your views, it will not necessarily rename those variables. I haven't done an empirical experiment to see if there are certain cases where it does or does not pick those up, but I have definitely had several cases where it renamed the more strongly typed variables in the server-side code (DAL and Controllers) but not the views.

编辑:关于这种方法的一个警告。如果您在视图中使用 Razor,它不一定会重命名这些变量。我还没有做过实证实验,看看在某些情况下它是否会接受或不接受这些,但我肯定有几种情况,它重命名了服务器端代码(DAL 和控制器)中的强类型变量) 但不是意见。

回答by jp2code

Depending on what the default keyboard mapping scheme you use, you might be able to get away with simply pressing F2to call Renamebefore ever typing in the text change.

根据您使用的默认键盘映射方案,您可能只需在输入文本更改之前按F2调用Rename就可以逃脱。

If your version of VS does not have the F2mapped, you can rename that from within the program's Main Menu:

如果您的 VS 版本没有F2映射,您可以从程序的主菜单中重命名它:

  • Tools>> Options
  • In Options Dialog, expand Environment, and then select Keyboard
  • Type File.Renameinto the "Show commands containing"Search box
  • Select the "Press Shortcut keys" TextBox and click F2
  • Click Assign
  • 工具>>选项
  • 在选项对话框中,展开环境,然后选择键盘
  • 键入File.Rename“显示命令包含”搜索框
  • 选择“按快捷键”文本框并单击 F2
  • 单击分配

screenshot

截屏

回答by Brett

In VS 2008 using C# you can just hit F2, not sure about other versions/languages, though. That also renames files in Windows Explorer if you're too lazy to right-click like I am.

在使用 C# 的 VS 2008 中,您可以点击F2,但不确定其他版本/语言。如果您懒得像我一样右键单击,这也会重命名 Windows 资源管理器中的文件。

回答by Brandon

I think the default is Ctrl+ ..

我认为默认值是Ctrl+ .

回答by Yuval

Ctrl + h work for me Good luck

Ctrl + h 对我来说有效 祝你好运

回答by Nordes

If you like, I would suggest that you try ReSharper, it provides a lot of functionality for refactoring your code.

如果您愿意,我建议您尝试ReSharper,它提供了许多重构代码的功能。

In ReSharper to rename a variable or function, simply use the shortcut CTRL+R, R.

在 ReSharper 中重命名变量或函数,只需使用快捷键CTRL+ R, R

(I know it may not be an answer to your question, but I think if you like using keyboard shortcuts that you will enjoy ReSharper).

(我知道这可能不是您问题的答案,但我认为如果您喜欢使用键盘快捷键,您会喜欢 ReSharper)。

回答by Gatica

On Visual Studio 2013, the listed shortcuts Ctrl+., F2, etc... did not work for me.

在Visual Studio的2013年,上市的快捷键Ctrl+ .F2等...没有对我来说有效。

I found that installing a Visual C++ Refactoringextension solved my problem.

我发现安装Visual C++ 重构扩展解决了我的问题。

I can now use Ctrl+R, which brings up a refactoring pop-up and allows me to select current scope by default, as well as other instances in the same file.

我现在可以使用Ctrl+ R,它会弹出一个重构弹出窗口,并允许我默认选择当前范围,以及同一文件中的其他实例。

Refactoring PopUp

重构弹出窗口

Here's an example of a variable I renamed in a function. There was another function there with the same variable name, which it suggests to rename...

这是我在函数中重命名的变量示例。那里还有另一个具有相同变量名称的函数,它建议重命名...

Refactoring Menu

重构菜单

回答by Jose Aedayodi

Quick Notes: Ctrl+ .works. But not if you hit backspace and then rename the variable name.

快速说明: Ctrl+.有效。但如果您按退格键然后重命名变量名称,则不会。

e.g. rename SenderEmail to senderEmail

例如,重命名小号enderEmail到小号enderEmail

If you highlight the Sin SenderEmail and replace the letter and then use Ctrl+ .you will see the popup to rename the variable. If you add a letter to the variable this still works. If you delete, it still works.

如果突出显示小号小号enderEmail并替换字母,然后用Ctrl+ .,你会看到重命名变量弹出。如果您向变量添加一个字母,这仍然有效。如果删除,它仍然有效。

However, and this is what I do a lot - if you hit backspace on the SenderEmail and make it enderEmail and then add an smaking it senderEmail and then hit Ctrl + . this will not work.

但是,这是我做了很多-如果你打的退格小号enderEmail并使其enderEmail,然后添加一个S ^使它小号enderEmail,然后按Ctrl +。这是行不通的。

Shift+Alt+F10also works in the same way.

Shift+ Alt+F10也能在相同的方式。

Hope this helps! :)

希望这可以帮助!:)

回答by msysmilu

After the rename: CTRL+.

重命名后:CTRL+.

Before the rename: CTRL+r,r

重命名前:CTRL+ r,r

VS 2017, Smart Tags enabled

VS 2017,启用智能标签