How to save format when pasting in Visual Studio 2015?

I have a switch case in a section of my function, and I need to reorder some cases to read the code better.

So, the code at the moment looks something like this:

switch(parameter) { case "foo" : {DoSomething; DoSomething; DoSomething; DoSomething; break } case "bar" : {DoSomething; DoSomething; DoSomething; DoSomething; break } .... case "alpha" : {DoSomething; DoSomething; DoSomething; DoSomething; break } case "beta" : {DoSomething; DoSomething; DoSomething; DoSomething; break } } 

So, I have hundreads of cases inside this switch statement, and I need to reorder most of them. And although reordering, for example, if I want to put case foo and case bar below cases alpha and beta. Simple Ctrl+c and Ctrl+v give me this output:

 switch(parameter) { case "alpha" : {DoSomething; DoSomething; DoSomething; DoSomething; break } case "beta" : {DoSomething; DoSomething; DoSomething; DoSomething; break } ...... case "foo" : {DoSomething; DoSomething; DoSomething; DoSomething; break } case "bar" : {DoSomething; DoSomething; DoSomething; DoSomething; break } } 

Reordering this text several times is a cumbersome task. Is there a way that can duplicate a string as it is in some other part of the code?

For example, I want all the text to remain on one line, as before,

  case "foo" : {DoSomething; DoSomething; DoSomething; DoSomething; break } case "bar" : {DoSomething; DoSomething; DoSomething; DoSomething; break } 
+8
c # visual-studio
source share
3 answers

You have a β€œfunction” Visual Studio that automatically formats the code for certain actions (the completed statement on ;, the completed block on } or Paste . Fortunately, these settings can be changed through the following settings page (they depend on the language):

Tools > Options > Text Editor > C# > Formatting

Then temporarily clear the Automatically format on insert check box .

Common C # formatting options

+21
source share

Since the question is about reordering in general, this can be useful as well.

To easily reorder code: select the code to move, then use Alt + UpArrow or Alt + DownArrow to move the selection up or down. For indentation of the selection, use Tab or Shift + Tab to move the indentation back.

+3
source share

Before pasting, turn off all automatic formatting.

-one
source share

All Articles