Visual Studio 2013: Format the Current Document or Selection

 

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!

Comments

  • Anonymous
    August 19, 2013
    Great

  • Anonymous
    August 28, 2013
    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

  • Anonymous
    February 11, 2014
    The comment has been removed

  • Anonymous
    February 28, 2014
    @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.

  • Anonymous
    March 20, 2014
    Agreed with LeeC. It isn't working for VB code either.

  • Anonymous
    May 05, 2014
    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.

  • Anonymous
    May 19, 2014
    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

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

  • Anonymous
    September 17, 2014
    "The key combination (Ctrl+K, Ctrl+D) is bound to a command (Format Document) which is not currently available." This from a pristine install of VS 2010 in a Win7 environment (corporate dictate). Something made it not available, MS ought to know why. Or has their own software become so complex they can't properly support it, it's up to bloggers and offshore resources?

  • Anonymous
    December 04, 2014
    The comment has been removed

  • Anonymous
    April 23, 2015
    Per LeeC above this feature is generally problematic. Visual Studio 2013 fails to properly format CSS in HTML documents with this command. It swaps arbitrarily between 2-4 tabs of indentation.

  • Anonymous
    May 11, 2015
    The comment has been removed

  • Anonymous
    May 19, 2015
    The comment has been removed

  • Anonymous
    August 13, 2015
    Just used this in 2013 and it changed the casing of somethings in the document.  Which of course broke the document.  Is there a way to prevent it from doing that? I never had the problem when using it in prior versions of vs.

  • Anonymous
    October 15, 2015
    Visual Studio 2015 is just the same when it comes to HTML auto-formatting. The version I had before (I think it was VS2012 but not sure) used to work well.