Tip/Trick: Custom formatting HTML in Visual Web Developer and Visual Studio 2005

 

Problem

 

You receive an HTML or ASP.NET page from another developer you work with whose html source formatting standards are different from your own (bad use of casing, inconsistent indenting, etc).

 

You want to quickly clean-up and format the HTML “the right way” – where “the right way” is naturally defined as own your personal preference (and naturally every developer thinks their preference is "right" <g>).

 

Solution

 

Visual Web Developer and Visual Studio 2005 include a rich HTML source formatting rules engine that enables you to configure exactly how you want HTML markup to look.

 

These HTML formatting rules are used by default whenever you add HTML elements or ASP.NET Server Controls within the WYSIWYG designer using the Toolbox.  You can also use these rules in source-view to select HTML you have imported into your project, and quickly format it with them. 

 

Simply highlight the region of HTML you want to apply the source-formatting rules to in the source editor, and then right-click and choose the “Format Selection” context menu item:

 

 

This will apply the HTML source formatting rules currently configured and clean up the HTML markup for you, without changing any rendering semantics:

 

 

You can easily customize the HTML source formatting rules to match your own particular preference by right-clicking within the html source editor and choosing the “Formatting and Validation” context menu option:

 

 

This will bring up a dialog that allows you to control the default capitalization and casing rules, attribute quoting, and self-terminating semantics of elements.  It also allows you to control when elements wrap to a new line within the page:

 

 

By default if your line is more than 80 characters in length, it will wrap the text onto a new line (for example: if you have a GridView with a lot of properties you might see this happen).  If you prefer, you can either uncheck this behavior or increase/decrease the character length to change the wrapping semantics.

 

You can also click the “Tag Specific Options” button to bring up a dialog that enables further element customization:

 

 

Specifically, it allows you to control the colorization, line breaks, and default tag termination semantics for every client-side HTML element and ASP.NET server control.  For example, if I wanted to change the line break semantics of <div> elements so that their inner content region didn’t have line breaks, I could do so like this (note the preview pane that shows you how the setting will affect things):

 

 

The per-tag colorization and bolding rules are useful if you want to specifically highlight either the foreground or background of important elements to make them stand-out more in the editor.  One scenario you might want to use this would be to highlight in red any “non-CSS recommended” element (like <tables>) to make you more aware when you are using them.

 

Note that if you don’t want to tweak individual elements, you can click the “Default Settings” treeview node on the left to change the defaults for elements globally (note: you could then go in and override individual elements on a per element basis like above if you want).

 

The VS source-formatter is smart and will correctly handle white-space rendering semantics for individual HTML elements.  For example: if you have a <a></a> or <td></td> elements it will be careful not to change any line-break or spacing semantics of the markup within it that would cause spacing because the rendering semantics changed.  If you format a selection of markup and see that a close tag hasn’t been moved to a separate line – it is because there is no space between the end of the preceding markup and the terminating tag, and as such VS is being careful not to change it to avoid changing the rendering semantics.

 

The HTML source formatting rules you’ve set will be used anytime the WYSIWYG designer adds new HTML elements to a page (for example: you drag/drop an element from the toolbox).  The VS 2005 WYSIWYG designer will not re-format any customer HTML changes you’ve made in source view yourself.  Unlike previous versions of VS, it does not modify or change any markup you’ve typed as you switch back and forth between design view and source view, or change content within the design surface.  The WYSIWYG designer only uses the source formatting rules when you add new elements from the toolbox.

 

Once you have the rules setup the way you want, you can also export and import them to other machines using the “Tools->Import and Export Settings” menu option.  This allows you to share your HTML source formatting rules either on the web or with other developers on your team.

 

Additional Links

 

Hope this helps,

 

Scott

 

Published Friday, July 07, 2006 11:34 AM by ScottGu

Comments

# re: Tip/Trick: Custom formatting HTML in Visual Web Developer and Visual Studio 2005

Friday, July 07, 2006 3:54 PM by Sonu Kapoor [MVP]
I have been using that feature since a while in VS2005 and I love it.

# re: Tip/Trick: Custom formatting HTML in Visual Web Developer and Visual Studio 2005

Friday, July 07, 2006 3:59 PM by Jon
how do I gert asp tags to format nicely like this http://www.superexpert.com/Books/AspNet2Unleashed/Chapters/Chapter3/Listing3_01.aspx

# re: Tip/Trick: Custom formatting HTML in Visual Web Developer and Visual Studio 2005

Friday, July 07, 2006 4:08 PM by Sahil Malik
Hey I use that trick a lot :) CTRLA-K-F .. almost a second nature now. Okay here's a "Please blog about" What is the best way to copy paste out of VStudio, without getting an extra line break at the end of each statement?

# re: Tip/Trick: Custom formatting HTML in Visual Web Developer and Visual Studio 2005

Friday, July 07, 2006 4:40 PM by Roger
This is a great feature and maybe worth the price of admission alone for VS2005. This also works with C# code. Speaking of which, It's always puzzled me that the default settings for code formatting don't match Microsoft's own design guidelines, specifically regarding line breaks around braces. FDG says no breaks before open braces: class FooBar { } All the class files generated by studio follow this including "designer.cs" files and typed datasets. However, the default formatting setting in VS2005 is to use separate lines: class FooBar { } It always seemed strange to me that my code-behind classes uses line breaks, but my designer-generated classes don't! Personally, I like separate lines, but there definately seems to be two camps on this within Microsoft! Maybe you could shed some light :-) Roger

# Tip/Trick: Custom formatting HTML in Visual Web Developer and VS.NET

Friday, July 07, 2006 5:44 PM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# Tip/Trick: Custom formatting HTML in Visual Web Developer and Visual Studio 2005

Saturday, July 08, 2006 1:14 AM by My Blog

# re: Tip/Trick: Custom formatting HTML in Visual Web Developer and Visual Studio 2005

Saturday, July 08, 2006 2:31 AM by Mohamed Rashed

Ctrl+k >> Ctrl+D do all for the document ;)

# Custom formatting HTML in Visual Web Developer and Visual Studio 2005

Saturday, July 08, 2006 3:12 AM by Kent J. Chen's Weblog

# re: Tip/Trick: Custom formatting HTML in Visual Web Developer and Visual Studio 2005

Saturday, July 08, 2006 3:36 AM by shortcut

How about CTRL K+D that's 1-key shorter! A+K+F

# Interesting Finds: July 8, 2006

Saturday, July 08, 2006 11:32 AM by Jason Haley

# re: Tip/Trick: Custom formatting HTML in Visual Web Developer and Visual Studio 2005

Saturday, July 08, 2006 4:19 PM by Nicholas

While I really like this feature, it needs to be more complete. Why am I not able to specify that linebreaks should be placed after properties inside of ASP.Net control tags? It makes code much easier to read..

# re: Tip/Trick: Custom formatting HTML in Visual Web Developer and Visual Studio 2005

Sunday, July 09, 2006 9:25 AM by Granville Barnett

I must of been living under a rock for the last 6 or so months I've never came across this feature!! looks really cool though!!

# Link Listing - July 9, 2006

Sunday, July 09, 2006 11:54 PM by Christopher Steen

A slider client control for Atlas [Via: bleroy ] ADO.NET and SQL Server Everywhere [Via: alexbarn...

# Tip/Trick: Fast HTML Editor Navigation within VS 2005

Tuesday, July 11, 2006 2:36 AM by ScottGu's Blog

Problem You have a large ASP.NET page that you are working on within Visual Studio, and you want to quickly

# re: Tip/Trick: Custom formatting HTML in Visual Web Developer and Visual Studio 2005

Tuesday, July 11, 2006 2:52 AM by ScottGu

Hi Roger,

The debate over whether braces should start on the same line as the statement or the line below it has been going on since the beginning of time within Microsoft.  It was probably the hardest and most contentious design decision we made the first month that we started coding ASP.NET. :-)

Thankfully you can go into VS and change the defaults if you want to.  That way hopefully everyone can be happy.

# re: Tip/Trick: Custom formatting HTML in Visual Web Developer and Visual Studio 2005

Friday, July 14, 2006 3:43 AM by Michiel

I'm working on the Data Tutorials by Scott Mitchell on www.asp.net, and I noticed that VS will not correctly format my &lt;li&gt; tages, no matter what I do.

For &lt;ul&gt; I have separate closing tag, line break before and after, indent contents. For &lt;li&gt; I have separate closing tag, line break before and after.

It should look like this:

ul

 li content /li

 li content /li

/ul

but it ends up like this:

ul

 li content /lili content /li/ul

Is this a bug?

# ASP.NET 2.0 Tips, Tricks, Recipes and Gotchas

Tuesday, August 01, 2006 11:55 AM by ScottGu's Blog

This page lists some of the more popular &amp;ldquo;ASP.NET 2.0 Tips, Tricks, Recipes and Gotchas&amp;rdquo;

# re: Tip/Trick: Custom formatting HTML in Visual Web Developer and Visual Studio 2005

Tuesday, December 05, 2006 5:36 AM by Max

While this seems a very useful tool, it does not seem to work properly for me. I have set e.g. the setting for the p tag to be "Before opening, within, and after closing", but if I add a line

<p>Lorem Ipsum</p>

and reformat it, it will be reformatted to:

<p>

  Lorem Ipsum</p>

(note the closing tag not it's own line)

Is there any solution for this?

# re: Tip/Trick: Custom formatting HTML in Visual Web Developer and Visual Studio 2005

Wednesday, December 06, 2006 12:11 AM by ScottGu

Hi Max,

It is actually doing the right behavior above.  The reason it doesn't separate the closing p element is because adding a space there would potentially change the whitespace semantics of the rendering.

Hope this helps,

Scott