Visual Studio uses XHTML tag closing for HTML5 tags - visual-studio

Whenever I type out tags that don't require an end tag, Visual Studio adds a forward slash to it, as if they were XHTML.
I think this is unclean and annoying, and I'm hoping anyone can tell me how to fix or work around this issue.
It even happens when I complete <!DOCTYPE html with a >, and it'll form <!DOCTYPE html />.

For completeness I'm posting this answer. Although we're in 2014 now, with Visual Studio 2013 and HTML5 is getting more and more supported by the major browsers, Visual Studio still behaves the same as ... like always?
The option is just a bit different than in 2010 I assume (based on accepted answer).

What you could do is disable the auto closing tag option in Visual Studio.
Go to Tools >> Options >> Text Editor >> HTML >> Formatting
Uncheck the "Auto insert close tag" option.
This will now allow you to type <br> (for example) and it won't auto-complete it as <br />

For Visual Studio 2012, go to Tools -> Options -> Text Editor -> HTML -> Formatting and click Tag Specific Options.
Under Default Settings -> Client tag does not support contents, set Closing tag to No closing tag.

Related

How to add custom HTML Tags to Visual Studio and Avoid Squiggly Lines

I'm using Visual Studio 2013 to create raw HTML pages (i.e. index.html).
In my HTML I am implementing custom HTML tags. As such, Visual Studio underlines my custom tags with squiggly lines (due to the warning of "Unknown element ...").
In hopes to get rid of the squigglies, I added my custom tags to the list of "Tag Specific Options" via Tools > Options > Text Editor > HTML (Web Forms) > Formatting > Tag Specific Options. However, the squiggly lines persist.
Is there a way to inform VS2013 that my custom HTML tags are valid (i.e. keep VS2013 from underlining my custom tags with squigglies)?
Visual Studio 2013 Update 4 removes the validation in .html files, but not in .aspx files.
Support for custom elements, polymer-elements and attributes
We no longer validate unknown attributers for custom elements as there
will be many custom made tags in different frameworks. So there will
no longer be squiggles under the unknown elements.
— Announcing new Web Features in Visual Studio 2013 Update 4 RC
Download Visual Studio 2013 Update 4.
Validation is only removed in the HTML editor, not the HTML (Web Forms) editor. This means that, by default, .html files will not have custom element validation, but .aspx pages will. If, like me, you don't think this makes sense, show your support by voting for custom elements in .aspx files.
Hmm... what are you trying to accomplish? Keep in mind that if you could somehow make visual studio ignore the unknown element, that's no guarantee browsers looking at your page will be able to translate the element too.
Are your pages HTML5? if so you've got a shot at making it work. but you'll have to define it in client code. Here is source.
But most browsers don't support it yet! look at here elements.
In the case of making Visual Studio recognise the tags you could take this approach:
1) Go to: Tools > Options > Text Editor > HTML (Web Forms) > Formatting
2) Press "Tag Specific Options"
3) Select "Client HTML Tags" in the new window's treeview
4) Press "New Tag" below the treeview to add a new tag

Disable Visual Studio code formatting in Razor

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!
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
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
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.
What did the trick for me was pressing CTRL+Z after pasting, then the formatting is removed.
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
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.
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
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.
I found these settings in Resharper 2019, which have improved my experience:
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

Make Visual Studio 2013 use Self-Closing Tags for XAML

I've just started messing around with WPF, and something I've noticed is that when writing in Visual Studio's XAML editor, tags that I'd like to be formatted as self-closing tags (for example <Setter />) always get auto-completed to have a separate closing tag, like <Setter></Setter>. Is there a way to change this behavior, or am I stuck reformatting those tags by hand?
I do not see one in the Visual Studio options but there is one in the Resharper options.
Resharper menu -> Code Editing -> XAML -> Editor -> Check "Always create self-closed tags in code completion"

ReSharper / VS 2010: How to disable HTML closing tag insertion

ReSharper Full Edition v6.0.2202.688
Visual Studio 2010 Professional SP1
As an example, when I type <div and hit space or close the start tag ReSharper inserts the closing </div> tag. I don't want it to do this. I know there is a corresponding Visual Studio setting which does the same thing, which I have already disabled but makes no difference as ReSharper seems to do it in its own way.
VS setting:
Tools > Options > Text Editor > HTML > Formatting > "Auto insert close tag"
I have scoured the web for answers and the closest thing I can find is that there may be a bug in some versions of ReSharper which causes this, but I have been unable to properly confirm that.
This was fixed to respect VS option in 6.1. Please upgrade.

How to disable Visual Studio "smart" way to replace IDs in markup

From the hundreds of Visual Studio options, is there one to disable the way VS replace the IDs in markup when copy&paste from other control?
Almost always only need to change a few characters but VS thinks he is being smart replacing all the ID
Go into Tools > Options > Text Editor > HTML > Miscellaneous and uncheck "Auto ID elements on paste in Source view"
source:
How do you customize the copy/paste behavior in Visual Studio 2008?
but in 2010 it is the same

Resources