VisualStudio: Shortcut for when Renaming Variable

Visual StudioKeyboard Shortcuts

Visual Studio Problem Overview


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.

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

Visual Studio Solutions


Solution 1 - Visual Studio

These are known as Smart Tags.

You can use CTRL+.

Shift+Alt+F10 is another option.

Solution 2 - Visual Studio

By way of an updated answer...

Update for Visual Studio 2022

Same behavior as 2019.

Update for Visual Studio 2019

Same behavior as 2017.

Update for 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 2015

In Visual Studio 2015, if you click on the variable and use Ctrl + r, Ctrl + r it 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).

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.

Solution 3 - Visual Studio

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

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

  • Tools >> Options
  • In Options Dialog, expand Environment, and then select Keyboard
  • Type File.Rename into the "Show commands containing" Search box
  • Select the "Press Shortcut keys" TextBox and click F2
  • Click Assign

screenshot

Solution 4 - Visual Studio

Ctrl + h work for me Good luck

Solution 5 - Visual Studio

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.

Solution 6 - Visual Studio

I think the default is Ctrl + ..

Solution 7 - Visual Studio

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

In ReSharper to rename a variable or function, simply use the shortcut 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).

Solution 8 - Visual Studio

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

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

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.

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

Solution 9 - Visual Studio

For windows installation you need to double click the variable name (it will highlight all the instances) then press alt and double click the variables where you want to rename. Your will get all teh cursors and now you can delete the old name and give new name.

Solution 10 - Visual Studio

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

e.g. rename SenderEmail to senderEmail

If you highlight the S in 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.

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

Shift+Alt+F10 also works in the same way.

Hope this helps! :)

Solution 11 - Visual Studio

After the rename: CTRL+.

Before the rename: CTRL+r,r

VS 2017, Smart Tags enabled

Solution 12 - Visual Studio

Searched for this in 2019... What worked for me was to go to Options and add the shortcut on the Refactor.Rename command, like this

Solution 13 - Visual Studio

I use ctrl + f

It opens a window you can use to find and replace all occurrences of a word. You can replace the word in "Current Document", "All Open Documents", "Current Project", or "Entire Solution" with the drop down menu below the button that says "Replace All" when you hoover over it.

I hope that helps.

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionInfoStatusView Question on Stackoverflow
Solution 1 - Visual StudioAhmad MageedView Answer on Stackoverflow
Solution 2 - Visual StudiojoshmcodeView Answer on Stackoverflow
Solution 3 - Visual Studiojp2codeView Answer on Stackoverflow
Solution 4 - Visual StudioYuvalView Answer on Stackoverflow
Solution 5 - Visual StudioBrettView Answer on Stackoverflow
Solution 6 - Visual StudioBrandonView Answer on Stackoverflow
Solution 7 - Visual StudioNordesView Answer on Stackoverflow
Solution 8 - Visual StudioGaticaView Answer on Stackoverflow
Solution 9 - Visual StudioAli Tauseef RezaView Answer on Stackoverflow
Solution 10 - Visual StudioJose AedayodiView Answer on Stackoverflow
Solution 11 - Visual StudiomsysmiluView Answer on Stackoverflow
Solution 12 - Visual StudioCharlie ArayaView Answer on Stackoverflow
Solution 13 - Visual StudioOverloaded_OperatorView Answer on Stackoverflow