Partager via


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

  • Anonymous
    January 10, 2008
    Ctrl-K, Ctrl-D also works to format the current file without the select all required.

  • Anonymous
    January 10, 2008
    Questo shortcut è davvero utilissimo: CTRL+K+F premuto all'interno di Visual Studio con attiva una finestra...

  • Anonymous
    January 10, 2008
    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++.

  • Anonymous
    January 10, 2008
    Here's a macro that will format an entire solution for you: http://www.kodefuguru.com/post/Format-Solution.aspx

  • Anonymous
    January 16, 2008
    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.

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

  • Anonymous
    March 13, 2008
    The comment has been removed

  • Anonymous
    April 01, 2008
    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

  • Anonymous
    June 05, 2009
    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)

  • Anonymous
    March 04, 2010
    markhsch, You must keep pressing the CTRL key on both K and D keys in order for it to work.

  • Anonymous
    October 15, 2010
    use CTRL +K ,D is best command for formatting with out selection

  • Anonymous
    December 14, 2010
    Hi what are the same shortcut keyss in c++ 6 ?

  • Anonymous
    March 01, 2011
    The comment has been removed

  • Anonymous
    May 17, 2011
    Thanks mate, this was extremely  helpfull!

  • Anonymous
    June 16, 2011
    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.

  • Anonymous
    September 28, 2011
    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.  

  • Anonymous
    July 10, 2015
    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