Visual Studio 2013: Format the Current Document or Selection

Tips Search

Visual Studio 2013: Format the Current Document or Selection

  • Comments 8

 

NOTE: This is an older feature. I’ve updated the information for VS2013.

 

 

Format Document

This one is a little known feature you can take advantage of when working with your code. Assuming you have some unformatted code:

5-16-2012 12-14-43 PM

 

5-16-2012 12-10-39 PM

 

 

You can format the entire document by going to Edit -> Advanced -> Format Document (CTRL+K, CTRL+D) to get this:

5-16-2012 12-16-13 PM

 

5-16-2012 12-18-01 PM

 

 

 

Format Selection

You can also do this with just a selection of code:

5-16-2012 12-21-01 PM

 

Go to Edit -> Advanced -> Format Selection (CTRL+K, CTRL+F) to get this:

5-16-2012 12-23-33 PM

 

 

 

Formatting Options

I won’t go deep into it here but it’s important to remember that the automated formatting follows the default rules you can easily change by going to Tools | Options | Text Editor | [Language] | Formatting in most cases. For example, to change the HTML rules go to Tools | Options | Text Editor | HTML | Formatting:

5-16-2012 12-27-47 PM

 

In the case of HTML, you can go pretty deep into the formatting options by clicking on the Tag Specific Options button:

5-16-2012 12-29-32 PM

 

 

 

Finally

Formatting the document or selection is a great way to make your code more readable. Enjoy!

  • Great

  • I wonder how many people realize that these commands are based upon the WordStar command set?   I use them all the time and they always make me think of WordStar.

    en.wikipedia.org/.../WordStar

  • Document formatting in Visual Studio 2013 sucks.  Yet another instance where Microsoft abandoned a feature, instead of fixing its bugs.  In VS 2013, if I paste minified HTML, and format the document, nothing happens--no thing!  Thanks, Microsoft.  I heard Mads call this "getting out of your way, because it never worked right in the first place."  I call it removing one the product's most useful features.  Document formatting in Visual Studio 2013 sucks!

  • @LeeC

    Are you pasting into a regular HTML file? I've noticed that regular HTML files get opened with the HTML Editor which doesn't follow the Tag Specific Options in Visual Studio 2013. If you right click the HTML file > Open With and choose HTML (Web Forms) Editor, you'll get all the formatting and tag specific options again.

  • Agreed with LeeC. It isn't working for VB code either.

  • It doesn't seem to to do anything with C# code, either.  Whether I use the menu entry or the keyboard shortcut, the indentation isn't fixed, and spacing isn't modified to match my settings.  It does absolutely nothing.

  • formatting is not working it does not much. When I use telerik justcode it does awesome formatting, Would be nice if Visual Studio could format code so it more readable

  • This was working. It doesn't any more. I was checking to see if anyone knew how to get it back.

Page 1 of 1 (8 items)
Leave a Comment
  • Please add 5 and 5 and type the answer here:
  • Post