Tuesday, August 7, 2012

Visual Studio Tips and Tricks, Part II: Format Spacing

Eric ReidThis is part 2 of a 3-part series on Visual Studio Tips and Tricks. These features are available in various versions of the Visual Studio family, including Visual Web Developer and Visual C# Express.

Visual Studio products will typically help with formatting the spacing of your code (including C#, HTML, CSS, etc.) by adding its form of standard spacing as you add line breaks or spaces within certain common structures – nesting indents for nested code blocks or tags, or adding spaces after some characters. However, it's easy (with copy-and-paste or tinkering) to get these out of whack.

To apply Visual Studio's standard spacing to your code you can use the Format Document or Format Selection features to update either the entire document or just your selected code.

The HTML code above is clearly using non-standard spacing.

Format Selection

To quickly format the spacing for the selected code:
  1. Select the code to format.
  2. Format the selection's spacing by either using the menus or shortcut keys:
    • Menu system Edit menu, Advanced submenu, Format Selection
    • OR Shortcut keys CTRL+K, then release and CTRL+F
Using Visual Studio's Format Selection feature.

The HTML code after the Format Selection feature has been applied. (Note that only the selected code, and those lines immediately before and after, have been updated.)

Format Document

To quickly format the spacing for the whole document, by using either the menus or shortcut keys:
  • Menu system Edit menu, Advanced submenu, Format Document
  • OR Shortcut keys CTRL+K, then release and CTRL+D

The HTML code after the Format Document feature has been applied. (Note all lines have been updated.)

No comments:

Post a Comment