Welcome to MSDN Blogs Sign in | Join | Help

Mark Schmidt's Abode

On Programming, Writing, Gaming, Fitness, Life

News

  • Have a Question?

    Click here to chat with me

    XBox Live GamerTag


    Twitter



    The Mark Cam


    My MoBlog

    www.flickr.com
    This is a Flickr badge showing public photos from codepunk. Make your own badge here.

    Community-Credit

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!

Posted: Thursday, January 10, 2008 1:16 PM by markhsch
Filed under: , ,

Comments

Doug said:

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

# January 10, 2008 4:29 PM

Maurizio Tammacco's blog said:

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

# January 10, 2008 5:02 PM

markhsch said:

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++.

# January 10, 2008 5:25 PM

Chris Eargle said:

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

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

# January 10, 2008 6:41 PM

Raj Kaimal said:

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.

# January 16, 2008 5:50 PM

Franci Penov said:

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

# February 7, 2008 2:14 AM

Fosna said:

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

# March 13, 2008 2:46 PM

Erik said:

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

# April 1, 2008 5:20 PM

A_Li_N said:

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)

# June 5, 2009 1:47 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker