Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve format while pasting in Visual Studio 2015?

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

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 while reordering, for example if I want to put case foo and case bar below cases alpha and beta. A simple Ctrl+c and Ctrl+v gives me an output like this:

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 }    } 

Rearranging this text multiple times is a cumbersome task. Is there a way a can duplicate a line as it is in some other part of the code?

For example I want the entire text to remain in a single line as it was before,

  case "foo" : {DoSomething; DoSomething; DoSomething; DoSomething; break }   case "bar" : {DoSomething; DoSomething; DoSomething; DoSomething; break }   
like image 770
agenthost Avatar asked Jan 28 '16 19:01

agenthost


People also ask

How do you paste with formatting code in Visual Studio?

Ctrl+V, Edit > Paste, Right Click > Paste. It should all just work!

How do I copy formatting in Visual Studio?

Go to Tools -> Options. type copy in the search box. Under Text Editor -> Advanced ... Check Copy rich text on copy/cut.

How do I fix the format in Visual Studio?

As long as your code is syntactically correct (i.e. no stray brackets or End Ifs without matching Ifs), Visual Studio will reformat your whole file with one key chord: Hold down the Control key and then press K, followed by D. Boom! Everything looks pretty again.


1 Answers

You're experiencing a "feature" of Visual Studio that auto-formats code on certain actions (completed statement on ;, completed block on }, or on Paste. Fortunately, these preferences can be changed via the following settings page (they are language-specific):

VS 2015

Tools > Options > Text Editor > C# > Formatting

Then temporarily uncheck the Automatically format on paste option.

General C# formatting options

VS 2017/2019

In Visual Studio 2017 and 2019, the "Formatting" options moved underneath a new "Code Style" menu and added some extra settings:

enter image description here

like image 100
Cᴏʀʏ Avatar answered Sep 21 '22 04:09

Cᴏʀʏ