174

I prefer my own style of code formatting as opposed to Visual Studio's default settings. I've turned off auto-formatting options in Tools→Options. In most cases, it works.

However, after using any of the built-in refactorings, Visual Studio clobbers my settings with its default settings. How do I keep VS from doing that?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Maciej
  • 10,423
  • 17
  • 64
  • 97

17 Answers17

120

I had this problem while writing VB in an aspx page.

The solution was to go to 'Tools > Options > Text Editor > Basic > VB Specific' and turn 'Pretty Listing' OFF.


Note - in Visual Studio 2015 this can be found at:

Tools > Options > Text Editor > Basic > Advanced

Paddy
  • 33,309
  • 15
  • 79
  • 114
Tango91
  • 1,209
  • 1
  • 8
  • 2
  • I wish I could bind these checkboxes easily to any short key. – Mike de Klerk Sep 03 '15 at 09:33
  • 1
    Thanks, this worked for me. The "prettying" is incredibly bad, with function parameters "tucked underneath" the function name, an inexplicable choice when tabular vertical alignment is what anyone who works in communications can tell you is easiest to apprehend. – Chris Fox Mar 19 '16 at 08:39
  • 77
    i'm just going to pause to admire the beauty of having an option tree hierarchy that goes `text editor -> basic -> advanced`.. 'oh yeah you want the advanced section'? That's under basic – fostandy Jul 10 '16 at 20:25
  • 21
    @fostandy, I realize you probably know this, but `basic` is the name for the language, not the section of options, as C# is also listed there. – Lightfire228 Oct 03 '16 at 01:48
  • 1
    FYI It might be under 'Visual Basic" if you are using newer versions of VS – dizad87 Mar 28 '23 at 21:22
37

Disable pretty listing. It is the option that re-formats what you are doing. I had the same discomfort with it and after doing it, my code stays how i want and it doesn't go back to previous settings.

enter image description here

RaRdEvA
  • 694
  • 7
  • 12
12

As suggest by @TheMatrixRecoder took a bit of finding for me so maybe this will help someone else. VS 2017 option can be found here

Unitick these options to prevent annoying automated formatting when you places a semicolon or hit return at the end of a line.

Tim Newton
  • 817
  • 1
  • 11
  • 24
9

In my case, it was ReSharper.

Test if ReSharper

StackOverflow: How can I disable ReSharper in Visual Studio and enable it again?

Prevent ReSharper from reformatting code

StackOverflow: Is there a way to mark up code to tell ReSharper not to format it?

Update 2017-03-01

It was ReSharper in the end:

enter image description here

Update 2020-12-18

On the latest version of ReSharper, there are more options: untick everything on this page, and ensure all dropdowns are set to the equivalent of None.

ReSharper "typing assist" is like a 3-year-old trying to "help" build a card castle. A simple backspace or an enter key will (poorly) reformat entire blocks of code, requiring it to be undone or painfully formatted back to the original.

And if that is not enough, this is the bit that adds delays when typing so sometimes it feels like trying to run in skis.

Contango
  • 76,540
  • 58
  • 260
  • 305
  • 1
    Agreed, the braces were always in the wrong place, and it literally stomped on the code with a heavy shod boot every time you hit a semicolon. Not cool. – Contango Mar 15 '17 at 16:43
8

You might have had Power Tool installed.

In this case you can turn it off from 'Tools > Options > Productivity Power Tools > PowerCommands > General'

enter image description here

Efreeto
  • 2,132
  • 1
  • 24
  • 25
5

The reformat on semicolon or closing brace cannot be turned off. I find it infuriating the Microsoft would have the temerity to tell anyone how to format code; the most illegible code I have ever seen was while working there.

I want adjacent assignments to be vertically aligned; VS reformats them to one space on either side of the equal sign irrespective of the length of the variable on the left. This is intolerable. And turning it off on the editor options is ignored; given comments like the opener above I am certain this is deliberate.

Consistency is only a virtue when it leads to desirable outcomes. This is not one.

Chris Fox
  • 641
  • 6
  • 10
4

VS2015 settings that helped me prevent auto formatting:

(and Tools > Options > Text Editor > Basic > Advanced, just like Tango91 suggested)

enter image description here

FrenkyB
  • 6,625
  • 14
  • 67
  • 114
3

In VS2017 you can change it after selecting your coding language in the settings menu. There is an option called "new Lines" in the "Formatting"-submenu.

3

I see that many answers here solve the problem for a specific situation.

For my situation I continually found that the IDE would automatically format JavaScript code within an ASP page.

To disable, I unchecked this box: enter image description here

In addition, I found it very helpful to use the search query in the toolbar (CTRL+Q) and just search for the text format on paste. As you can see with the scroll bar, there are quite a few options to check!

enter image description here

Brien Foss
  • 3,336
  • 3
  • 21
  • 31
2

I doubt that you can disable re-formatting after refactoring. Refactoring changes code and since it's only text I doubt what you'd want is that it just dumps unformatted text into your source. Wouldn't it be a little easier to just set the code style VS adheres to to the style you like and follow?

Joey
  • 344,408
  • 85
  • 689
  • 683
  • 1
    Sometimes the coding style is not algorithmizable. Sometimes it is willfully inconsistent because one contributes to a project with inconsistent coding style and wants to adhere to the coding style of the specific piece of code being edited. – Palec Dec 06 '15 at 01:41
1

Follow TOOLS->OPTIONS->Text Editor->CSS->Formatting Choose "Compact Rules" and uncheck "Hiearerchical indentation"

1

It can be the case of Clang Format. Previously, the entire file is automatically formatted on file save, and it drove me nuts (for the repositories which Clang Format is not enabled).

Such behavior is gone after turning "Tools -> Option -> LLVM/Clang -> ClangFormat -> Format On Save -> Enable" to False.

ClangFormat Format On Save

Tomoyuki Aota
  • 867
  • 9
  • 18
1

I was pissed off every time I pasted anything in cshtml file in Visual Studio 2015, 2017. I tried different settings and finally found the proper one: Options => Text Editor => HTML => Advanced => Paste (Format on paste) => False enter image description here

Pal
  • 756
  • 2
  • 10
  • 20
0

In addition to Tango's answer for the actual solution, there may be people actually want to stay current with auto-formats but not have it screw up your relevant changes. I would suggest that you modify the file to have auto-format activate, check in those changes, then proceed with the actual changes you wish to make.

That way your code can stay up to date, but your check in will be relevant.

Rudy Scoggins
  • 431
  • 3
  • 8
0

You might need to check on Tools-> Options-> Xaml Styler -> Format xaml on save

enter image description here

0

On the navigation options at the top select "Tools" and then select "Manage Extensions".

I had CodeMaid, Visual Assist, and Rosalyn installed. Seems like an update start to cause these auto formatting issues. I disabled them and do this fixed it.

minTwin
  • 1,181
  • 2
  • 21
  • 35
0

You can tweak the settings of the code formatting. I always turn off all extra line breaks, and then it works fine for how I format the code.

If you tweak the settings as close as you can to your preference, that should leave you minimal work whenever you use refactoring.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005