Visual Studio Tip : AutoFormat Your Code


Has this ever happened to you? You find some code on the Net so you copy and paste it into a source file within Visual Studio and notice that all the formatting is somehow messed up. For example. some lines might be indented while others are not.

To have Visual Studio fix it all up nice and tidy for you, select the text, hold CTRL and hit K followed by F (CTRL+K-F). What I usually do is do a CTRL-A to select all text then do the CTRL+K-F combo. Voila!

Comments (17)

  1. Doug says:

    Ctrl-K, Ctrl-D also works to format the current file without the select all required.

  2. Questo shortcut è davvero utilissimo: CTRL+K+F premuto all’interno di Visual Studio con attiva una finestra…

  3. markhsch says:

    Doug,

    Hmm, I try CTRL+K, CTRL+D and I get a message in the status bar saying "The key combination (CTRL+K,CTRL+D) is bound to command (Form&at Document) which is not currently available." Wonder why that is? Maybe that doesn’t work for C++.

  4. Chris Eargle says:

    Here’s a macro that will format an entire solution for you:

    http://www.kodefuguru.com/post/Format-Solution.aspx

  5. Raj Kaimal says:

    I think this depends on the "keyboard mapping scheme" you selected when you started VS the first time. For me Ctrl + E, D maps to  Edit.FormatDocument. Go to Tools – Options – Environment – Keyboard and type in Edit.FormatDocument to see what it maps to.

  6. FranciP says:

    Ctrl-K, Ctlr-F works on code files (C++, C#, VB), Ctrl-K, Ctrl-D works on document files (HTML, XML, XAML)

  7. Fosna says:

    Where do I find dialog box where I can change fomrating options such as curly prackets positiong?

  8. Erik says:

    Fosna, Here’s how to change formatting in Visual Studio 2008:

    * Go to Tools->Options…

    * Check "Show all settings"

    * Navigate to Text Editor -> C# (or whatever language) -> Formatting

  9. A_Li_N says:

    Kinda a related question:

    For some reason when I do auto-format and then save the file, my asp:Table, asp:TableRow, etc (all the table tags) get smooched smooched together.

    So if I had:

    <asp:Table>

      <asp:TableRow>

         <asp:TableCell>Content 1</asp:TableCell>

         <asp:TableCell>Content 2</asp:TableCell>

      </asp:TableRow>

      <asp:TableRow>

         <asp:TableCell>Content 3</asp:TableCell>

         <asp:TableCell>Content 4</asp:TableCell>

      </asp:TableRow>

    </asp:Table>

    after the format and save, it would look like this:

    <asp:Table>

      <asp:TableRow>

         <asp:TableCell>Content 1</asp:TableCell><asp:TableCell>Content 2</asp:TableCell></asp:TableRow><asp:TableRow>

         <asp:TableCell>Content 3</asp:TableCell><asp:TableCell>Content 4</asp:TableCell></asp:TableRow></asp:Table>

    Can you think of any reason it does this and maybe a way I could fix it?  All tags include the required items (Table tag includes the ‘runat="server"’, Cells and Rows do not)

  10. Nico says:

    markhsch,

    You must keep pressing the CTRL key on both K and D keys in order for it to work.

  11. sandip says:

    use CTRL +K ,D is best command for formatting with out selection

  12. Farzad says:

    Hi

    what are the same shortcut keyss in c++ 6 ?

  13. Dan says:

    Cheers for the post, i had a corrupted file that had an insane amount of line breaks in the code behind, manually deleting them all would be a nightmare!

    Thanks

  14. Adrian says:

    Thanks mate, this was extremely  helpfull!

  15. Dain says:

    For me on VS 2008 it was ctrl E, D to format document, and it was found under Edit>Advanced. Isn't working for me though.

  16. steve says:

    VS2010, C# – ON the menu under Edit>Advanced.  

    Ctrl+E, D  formats whole document

    Ctrl+E, F formats selected code

    Note – auto formatting only works if your code successfully parses/builds.  

  17. Sergey Vlasov says:

    There is now the Continuous Formatting extension that can automatically format code in background as you type:

    visualstudiogallery.msdn.microsoft.com/08afc8d1-4b74-44df-b012-f225ee9e9220