Does anyone know a visual studio keyboard short cut to swap around two sides of a statement?

Viewed 6138

Just wondering if anyone knows the keyboard shortcut to swap around two sides of a statement. For example:

I want to swap

firstNameTextbox.Text = myData.FirstName;

to

myData.FirstName = firstNameTextbox.Text;  

Does anyone know the shortcut, if there is one? Obviously I would type them out, but there is a lot of statements I need to swap, and I think a shortcut like that would be useful!

Feel free to throw in any shortcuts you think are cool!

My contribution would be CTRL + E, D - this will format your code to Visual Studio standards! Pretty well known I'm guessing but I use it all the time! :)

UPDATE

Just to let everyone know, using a bit of snooping of the article that was posted, I managed to construct a regular expression, so here it is:

Find:

{.+\.Text = myData\..+};

And replace with:

\2 = \1;

Hopefully people can apply this to their own expressions they want to swap!

7 Answers

swap-word is a VSCode extension which sounds like it would do what you want.

Quickly swap places two words or selections...

But I'm not sure if it is compatible with VS.

Since I was not happy with the answers where I need to enter complicated strings into the Visual Studio search/replace dialog, I wrote myself a little AutoHotkey script, that performs the swaps with only the need to press a keyboard shortcut. And this, no matter if you are in VS or in another IDE.
This hotkey (start it once simply from a textfile as script or compiled to exe) runs whenever Win+Ctrl-S is pressed

#^s Up::  
 clipboard := "" ; Empty the clipboard
 Sendinput {Ctrl down}c{ctrl up}
 Clipwait
 Loop, Parse, clipboard, `n, `r  ; iterates over seperates lines
 {   
  array := StrSplit(RegExReplace(A_LoopField,";",""),"=")  ; remove semicolon and split by '='
  SendInput, % Trim(array[2]) . " = " .  Trim(array[1]) . ";{Enter}"
 }
return 

Many more details are possible, e.g. also supporting code where lines end with a comma

...and I can put many more hotkeys and hotstrings into the same script, e.g. for my most mistyped words:

::esle::else    ; this 1 line rewrites all my 'else' typos

I recommend using the find-replace option in Visual Studio. IMHO the REGEX string is not that complicated, and moreover, you don't need to understand the expression in order to use it. The following regex string works for most programming languages:

([\w\.]+)\s*=\s*([\w\.]+)

For Visual Studio's you want to use $ argument in the replace text.

$2 = $1

Make sure to enable regex.

enter image description here

To do this in one shot, you can select a section of the document, and click the replace-all option.

  • Before:

     comboBoxAddOriginalSrcTextToComment.SelectedIndex = Settings.Default.comboBoxAddOriginalSrcTextToComment;
     comboBoxDefaultLanguageSet.SelectedIndex = Settings.Default.comboBoxDefaultLanguageSet;
     comboBoxItemsPerTransaltionRequest.SelectedIndex = Settings.Default.comboBoxItemsPerTransaltionRequest;
     comboBoxLogFileVerbosityLevel.SelectedIndex = Settings.Default.comboBoxLogFileVerbosityLevel;
     comboBoxScreenVerbosityLevel.SelectedIndex = Settings.Default.comboBoxScreenVerbosityLevel;
    
  • After:

     Settings.Default.comboBoxAddOriginalSrcTextToComment = comboBoxAddOriginalSrcTextToComment.SelectedIndex;
     Settings.Default.comboBoxDefaultLanguageSet = comboBoxDefaultLanguageSet.SelectedIndex;
     Settings.Default.comboBoxItemsPerTransaltionRequest = comboBoxItemsPerTransaltionRequest.SelectedIndex;
     Settings.Default.comboBoxLogFileVerbosityLevel = comboBoxLogFileVerbosityLevel.SelectedIndex;
     Settings.Default.comboBoxScreenVerbosityLevel = comboBoxScreenVerbosityLevel.SelectedIndex;
    

IMHO: It's better for a developer to learn to use the IDE (Integrated Development Environment), then to create new tools to do the same thing the IDE can do.

Related