154

This has been asked before: Why doesn't Visual Studio code formatting work properly for Razor markup?

But that question is a couple years old. And Razor formatting is still completely unacceptable. I have given up on fixing it.

Can I disable ALL Visual Studio formatting for CSHTML files? If not, can I disable all Visual Studio formatting entirely? Any 3rd party hacks I can use? There's got to be something!

I've upgraded to 2013 and the formatting is just as bad.

[EDIT]: 2015 is god-awful as well. I REALLY wish they would fix this.

[EDIT]: 2017 is god-awful as well. I REALLY wish they would fix this.

[EDIT]: 2019 is god-awful as well. I REALLY wish they would fix this. That said, Microsoft released a new formatting engine for .Net Core only.

[EDIT]: 2022 is... Well you know. If they ignore this 12 more years I may retire before seeing it fixed!

Paul
  • 5,700
  • 5
  • 43
  • 67
  • 17
    Its WORSE in 2013. – TombMedia Jan 16 '14 at 19:04
  • 3
    Seriously. There has to be something that can be done about this... – JustMaier Apr 18 '14 at 21:11
  • A number of people have directed readers of this OP to the ability to disable format on paste. This works if you are pasting a single line in. It won't reformat the lines around it. But paste an entire block of code in and you still get formatting. Not only that, it changed case on a number of items which made my code not compile. If everyone reading this would report feedback at the link mentioned by @Kusgun that would be appreciated! – Paul Mar 19 '15 at 15:17
  • 10
    It's WORSE in 2015 TOO! – Paul Sep 02 '15 at 17:27
  • 1
    Its like groundhogs day. I keep coming back to my own comments like I'm living the same pain points over and over again. Sigh. Maybe VS15 will have this fixed. – TombMedia May 27 '16 at 22:08
  • Seems fixed in vs 2015 update 3. – apr Jul 27 '16 at 09:29
  • Sorry I was wrong. Using tabs for indenting now works (no strange spaces), but the editor reformat (totally wrong way) the code when typing/pasting. – apr Sep 12 '16 at 08:56
  • In more complex Razor files, I find myself having to switch to using Visual Studio Code for much more basic text editing. Hitting Ctrl-z every other key in Visual Studio 2015 IDE is ridiculous. – Gordon Glas Nov 22 '16 at 15:32
  • 6
    Not fixed in vs 2017 rtm – apr Mar 17 '17 at 09:36
  • 7
    Not fixed in the year 2018. – Dirk Boer Mar 28 '18 at 15:51
  • I'm convinced whoever created the auto format in VS for CSHTML files is a sadist. – Batty McBat Apr 17 '19 at 22:23
  • 2
    Not fixed in the year 2019. – atilkan Sep 13 '19 at 12:53
  • Related open issue [#14271](https://github.com/aspnet/AspNetCore/issues/14271) and for Blazor (which is equally jacked up), issue [#14705](https://github.com/aspnet/AspNetCore/issues/14271). I can't say I'm surprised, though, formatting was also pretty unreliable in the bad-old-days of WebForms. – McGuireV10 Dec 23 '19 at 11:05
  • 2
    Not fixed in the year 2020 – Akın Tekeoğlu Oct 22 '20 at 09:03
  • 14
    Not fixed in the year 2021! – Ryan Lundy Jan 04 '21 at 15:38
  • 1
    It's 2022, new VS version, new year (plus new quarter), and the problem is still not solved. What I think is, at least the indentations for HTML part should be consistent with WPF XAMLs... (e.g. one tab (or equivalent spaces) indented from `<` if line break occurs in an opening tag) – Henry Kwon Apr 04 '22 at 01:12
  • 1
    8 years and this still makes my blood boil. I don't get why it's difficult to give an option to disable ALL whitespace formatting - at least we can evade your buggy code – Dirk Boer Oct 18 '22 at 22:07

10 Answers10

87

Under Tools -> Options -> Text Editor -> HTML -> Advanced

there is a key value item Paste which says "Format on paste" and has a boolean value next to it. Changing this to false has disabled formatting on paste for me in razor syntax.

I am using VS Professional 2013, Version 12.0.30110.00 Update 1

Droydn
  • 1,047
  • 8
  • 13
  • Thank you! The auto-(mal)formatting on paste was driving me crazy. This seems to have eliminated it. – Torrents Feb 23 '15 at 22:02
  • Awesome, this is exactly what is needed to drive this away! Thx! – Martin S Ek Aug 20 '15 at 07:24
  • I would up-vote you more then one time, This is a correct response for vs version 2013 up1 and newer. – Erik Simonic Jan 24 '16 at 11:33
  • 2
    Doesn't work in visual 2015? I've unchecked "format on paste" in HTML/advanced, but still get funky unformatting / crazy indenting when pasting. I'm considering to use another editor for cshtml files but then I'd loose C# autocompletion. This situation is crazy. – youen Apr 28 '16 at 09:01
  • 6
    Worked for me in VS2017. – Dave Dec 27 '17 at 22:28
  • Saved my day. This works for me VS 2019. I used Ctrl+Z every time paste in Razor file, but sometimes forgot that, making the file look like a lot changes in SVN/git. – Jiping Jun 18 '20 at 13:52
59

You cannot. This is built in "feature" of VS since the first .net version (visual studio 2002/2003).

There are tons of connect report about the formatting bugs, but Microsoft ignores them completly, or pushing the fix to the "next version". You can see an example Microsoft answer here.

You can post your own feedback to Microsoft Team here

Kuzgun
  • 4,649
  • 4
  • 34
  • 48
  • 1
    I was searching this in exasperation again, I don't know whether they've changed something, but Metroid Hunter's fix works for me in Razor http://stackoverflow.com/a/28053865/62829 – mattmanser Feb 16 '15 at 17:44
  • 4
    How can HTML in visual studio still suck so bad in 2015? – schmoopy Mar 25 '15 at 21:56
  • 1
    Actually it's worse in 2015 than the 2013 version (the same apply to C# where it will indent unrelated things each time you open a brace). I used to love visual studio but this is slowly killing it. – youen Apr 28 '16 at 09:11
  • 2
    Note: the connect links are all dead now – jrh May 29 '18 at 22:50
38

The places to look:

Tools -> Options -> Web Essentials -> HTML -> Auto-format HTML on Enter

(Requires installation of Web Essentials extension)

Set to False

Tools -> Options -> Text Editor -> HTML -> Advanced -> Format on Paste

Set to False

With these settings I have no trouble in Visual Studio 2013.

I used to have to press Ctrl-Z far too often, and if you forgot untangling the mess once your code was written and tested was a huge pain. I feel Microsoft could do alot to help users configure this as well as make it more accessible.

jsabrooke
  • 405
  • 7
  • 12
dazbradbury
  • 5,729
  • 5
  • 34
  • 38
27

What did the trick for me was pressing CTRL+Z after pasting, then the formatting is removed.

user2920118
  • 273
  • 3
  • 4
  • 3
    worked here...i was pasting a chunk including a – beauXjames Oct 29 '14 at 14:19
  • Upvoted. This works in VS 2015 :) No other solution (various settings under Tools > Options) worked for me, at least not when pasting content that is supposed to change between code/html in some way, i.e. pasting lines containing C# code followed by html tags or pasting html markup directly after lines with C# code. – savehansson Nov 18 '15 at 09:28
  • This always works, everywhere in VS. This is the **only** way to undo the mess that formatting did. (Or did you really clean it up manually??) I've already got used to it, but it's highly annoying. – ygoe Jan 05 '18 at 21:00
  • Astoundingly, in VS 2019, in a .razor file, hitting Control-Z after formatting _doesn't work anymore_. The bad formatting stays. – Ryan Lundy Jan 02 '21 at 09:59
11

In my case R# ended up being the culprit, can be disabled here:

ReSharper > Options > Code Editing > Razor > Editor & Formatting

https://www.jetbrains.com/help/resharper/2016.1/Reference__Options__Languages__Razor__Editor.html

hcoverlambda
  • 1,240
  • 1
  • 15
  • 21
  • This isn't included with Visual Studio though so you would have needed to install this separately. – Paul Aug 24 '16 at 17:27
  • 2
    The reason I mention it here is someone may be thinking that Visual Studio was doing the reformatting (like I did) when it could actually be R# (Which a lot of devs use). So if someone is unable to disable it and they have R# installed they would want to make sure it's disabled in both. – hcoverlambda Aug 24 '16 at 22:54
  • 5
    In R# 2017.3 these settings are at ReSharper | Options | Environment | Editor | Editor Behavior – Dark Daskin Dec 27 '17 at 13:12
  • 2
    In R# 2018.3 I had to also set **Editor Behavior, Auto-format on paste** to "None" – EM0 Mar 21 '19 at 16:15
7

Unfortunately the only way to prevent autoformatting on paste is to comment out the location where you're going to paste some new code and after the pasting operation to remove the commenting instructions.

Search & Replace seems not to trigger auto-formatting. But pasting reformats the entire code block.

This is terrible beyond words. It makes Visual Studio IDE virtually unusable for Razor editing. One has to use an external editor for Razor files.

What's even worse, even the latest Visual Studio 2013 Update 1 crashes while editing Razor views all the time.

guest
  • 71
  • 1
  • 2
6

The fun part is, you can disable C# formatting and HTML formatting but not Razor formatting.

I've unchecked "Tools > Options > Text Editor > C# > Formatting > Automatically format on paste" and the same for HTML.

Which results in the following fun experience in cshtml files:

  • Paste HTML code: no format
  • Paste C# code: no format
  • Paste Razor code: format everything wrong

And since there is no option to change the Razor options, you have to live with this I guess.

My "solution": paste and ctrl-z to undo the auto format

Christiaan Maks
  • 3,248
  • 3
  • 23
  • 27
3

I do not have a .Net Core project nor Blazor. So unfortunately I cannot test this. But a new formatting engine has been released by Microsoft for these languages.

https://devblogs.microsoft.com/aspnet/new-experimental-razor-editor-for-visual-studio/

Requires the latest edition of Visual Studio 2019.

Paul
  • 5,700
  • 5
  • 43
  • 67
  • Thanks, I enabled this and it seems to be working a lot better than the previous formatting engine. It no longer munges my formatting when I save, and it formats nearly everything correctly when I use the keyboard shortcuts (Ctrl-K, Ctrl-D in my case) to format the .razor file. – Ryan Lundy Jan 02 '21 at 10:10
  • Alas, on further use, it turns out that Intellisense breaks very easily with this new editor. – Ryan Lundy Jan 03 '21 at 07:48
  • That's too bad. Hopefully this new editor at least means they are finally working on it after 10 years without progress! – Paul Jan 04 '21 at 13:55
0

I found these settings in Resharper 2019, which have improved my experience: enter image description here

Stephen Oberauer
  • 5,237
  • 6
  • 53
  • 75
0

The below does not directly answer the posed question, but offers a potential fix that may be helpful.

In my situation, the auto formatting was only preforming exceptionally poorly for some files. This turned out to be related to the type of line feed used.

To resolve:

  • I enabled advanced save options
  • When saving the problematic files, I selected Windows (CR LF) as the line feed type

Supporting docs

After doing this, autoformat appeared to behave better