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





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

Comments (15)
  1. Roger Bonine says:

    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.…/WordStar

  2. LeeC says:

    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!

  3. Jordan says:


    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.

  4. Anonymous says:

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

  5. ADifferentLeeC says:

    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.

  6. bhanu korremula says:

    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

  7. acg says:

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

  8. Anonymous says:

    "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?

  9. MikMik says:

    Is there a way to prevent it from screwing up comments?

  10. Ken Kolano says:

    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.

  11. Michael Wood says:

    I just used it and it works great!

    Make sure you don't have separate tab settings for each language if you want them all to be the same.

    Then set Tools>Options>Text Editor>All Languages>Tabs



    To specify tab and indent settings

    Open the Text Editor options. (Tools / Options / Text Editor)

    For indenting, you can specify None (no indenting), Block (don't indent after an opening brace), or Smart (indent after an opening brace; the default) indenting options.

    To specify tab settings for all editors, expand All Languages and then click Tabs.

    To specify tab settings for a specific editor only, expand the appropriate language and then click Tabs for that language.

    For Tab and Indent size, type the number of spaces you want for automatic indents and between tab stops. For more information, see Tabs, All Languages, Text Editor, Options Dialog Box.

    Click OK.


    Note: If you set to NONE on indenting nothing will happen. I use SMART indents. I KEEP TABS

    This ALWAYS works. I do wish I could automatically format on save though. (I believe there is a tool out there someplace that will do that.)

    Also note: FORMATTING WILL NOT WORK if you have errors in your semantics. ie. in C# change the little "i" to an upper case "I" on an if(blah blah) statement and un-format it. This line and any lines below will not format as the editor doesn't know what you wanted to say.

    So you can also use formatting as another code inspection tool since poor code will not format either. ;~)



  12. Vijay Gullapalli says:

    Not sure what Michael Wood means by "This ALWAYS works". Set all the formatting up, opened an HTML document and ran the "Format Document" ^K^D command. NOTHING. Tried this on a javascript file as well. Same result. What a waste of time just trying to get this tool to do something simple and useful.

  13. Geoff says:

    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.

  14. Sam says:

    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.

Comments are closed.

Skip to main content