Disable Visual Studio code formatting in Razor
Asked Answered
C

10

158

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!

Clemenciaclemency answered 20/11, 2013 at 14:30 Comment(18)
Its WORSE in 2013.Kung
Seriously. There has to be something that can be done about this...Bentley
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!Clemenciaclemency
It's WORSE in 2015 TOO!Clemenciaclemency
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.Kung
Seems fixed in vs 2015 update 3.Diehard
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.Diehard
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.Androsphinx
Not fixed in vs 2017 rtmDiehard
Not fixed in the year 2018.Halyard
I'm convinced whoever created the auto format in VS for CSHTML files is a sadist.Corvese
Not fixed in the year 2019.Karlee
Related open issue #14271 and for Blazor (which is equally jacked up), issue #14705. I can't say I'm surprised, though, formatting was also pretty unreliable in the bad-old-days of WebForms.Tiemannite
Not fixed in the year 2020Sylas
Not fixed in the year 2021! 😷Abney
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)Crosstie
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 codeHalyard
Hello from the year of our lord 2024 and I can FINALLY report.. wait no it's still shiteBloodthirsty
H
60

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

Hageman answered 5/2, 2014 at 7:46 Comment(4)
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 https://mcmap.net/q/150768/-disable-visual-studio-code-formatting-in-razorGarage
How can HTML in visual studio still suck so bad in 2015?Messenger
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.Snowbound
Note: the connect links are all dead nowEzzo
M
88

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

Maziemazlack answered 20/1, 2015 at 19:44 Comment(6)
Thank you! The auto-(mal)formatting on paste was driving me crazy. This seems to have eliminated it.Albina
Awesome, this is exactly what is needed to drive this away! Thx!Skein
I would up-vote you more then one time, This is a correct response for vs version 2013 up1 and newer.Redeeming
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.Snowbound
Worked for me in VS2017.Soundproof
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.Nickelplate
H
60

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

Hageman answered 5/2, 2014 at 7:46 Comment(4)
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 https://mcmap.net/q/150768/-disable-visual-studio-code-formatting-in-razorGarage
How can HTML in visual studio still suck so bad in 2015?Messenger
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.Snowbound
Note: the connect links are all dead nowEzzo
C
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.

Cami answered 5/6, 2015 at 14:18 Comment(6)
There's no 'web essentials' area.Clemenciaclemency
Great answer! This has been bugging me for years, but no more!Fireboard
@Clemenciaclemency Web Essentials is a VS extension that enhances many things related to the HTML, CSS, JS editors. Installing it and disabling auto-format helps a lot.Sigmon
This should be marked as the answer. Especially the line about Web Essentials. Thank you @dazbradbury!Baerl
format on enter feature was costing me a lot of time in code re-factoring. Thanks for the help!Septuple
The second option doesn't fix it in VS2017 and the first doesn't even exist.Hellenhellene
C
28

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

Convex answered 16/7, 2014 at 13:49 Comment(4)
worked here...i was pasting a chunk including a <select> with a series of data-bind knockout attributes spread out over ~8 lines and it was killing the camel casing and removing the colons...was about to punt and retype it...then I did this Ctrl+Z and about squealed that it worked...@MirosReflate
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.Declass
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.Hellenhellene
Astoundingly, in VS 2019, in a .razor file, hitting Control-Z after formatting doesn't work anymore. The bad formatting stays.Abney
O
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

Overslaugh answered 24/8, 2016 at 16:10 Comment(4)
This isn't included with Visual Studio though so you would have needed to install this separately.Clemenciaclemency
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.Overslaugh
In R# 2017.3 these settings are at ReSharper | Options | Environment | Editor | Editor BehaviorTetragrammaton
In R# 2018.3 I had to also set Editor Behavior, Auto-format on paste to "None"Sheehy
L
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.

Lexielexigraphy answered 28/3, 2014 at 16:13 Comment(0)
C
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

Commit answered 27/8, 2014 at 9:20 Comment(2)
Me too.. but it's driving me mad!Dielle
Your solution is a work around, and takes minutes when editing large files.Buzzer
C
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.

Clemenciaclemency answered 2/9, 2020 at 13:16 Comment(3)
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.Abney
Alas, on further use, it turns out that Intellisense breaks very easily with this new editor.Abney
That's too bad. Hopefully this new editor at least means they are finally working on it after 10 years without progress!Clemenciaclemency
K
0

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

Kwapong answered 13/6, 2019 at 8:34 Comment(0)
A
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

Alic answered 22/12, 2021 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.