views:

643

answers:

4

In vim, if you, say, copy-paste a block of code into a file and it gets all wonky, you can hit == or =G to apply space formatting to the line (or to the end of the file). Is there a way to get this functionality in Visual Studio 2008? I found this gem:

Ctrl-K, Ctrl-F Applies the indenting and space formatting for the language as specified on the Formatting pane of the language in the Text Editor section of the Options dialog to the selected text.

I'm not sure what that means, and when I tested it, I got nothing.

+5  A: 

Try CTRL+K, CTRL+D

You can determine the current mapping by going to Tools -> Options -> Environment -> Keyboard -> type in "FormatDocument" and select the Edit.FormatDocument option. Look at the "Shortcuts for selected command" dropdownlist.

For example, in Visual Studio Team System 2008 Development Edition, with the environment setup for C#, I see 3 assignments:

  • CTRL+K, CTRL+D
  • CTRL+E, CTRL+D
  • CTRL+E, D

Alternately, from the menu: Edit -> Advanced -> Format Document.

Depending on the development settings you originally selected when you first ran Visual Studio, the above menu will change. Based on my observations, the above applies to General, C#, VB, and Project Management.

If you selected Web Development, the option will be Edit -> Format Document.

To reset your environment settings: Tools -> Import and Export Settings... -> Reset all settings -> Save current settings (optional) -> Choose a default collection of settings from the list presented.

Ahmad Mageed
This works not only in the code behind but also in the HTML and style sheets of a web application/web site.
Chris
I tried that and got "The key combination (Ctrl-K, Ctrl-D) is bound to command (Format Document) which is not currently available" Is there something else I need to do?
thepocketwade
also, there's not a Format Document on the Advanced menu
thepocketwade
@thepocketwade: the menu may differ. I updated my post with additional details.
Ahmad Mageed
+1  A: 

I think it should be, select All and then ctrl + K + D.

MemoryLeak
You don't need to select all first.
Brian Rasmussen
No need for selection though.
Robert Koritnik
A: 

For me Alt+F8 works (reformats the selection) but I have the "C-Key bindings" which are not the default.

Tassos
oops the correct key binding is Alt-F8 instead of Ctrl-Alt-F8 (edited)
Tassos
A: 

I just had this same problem. I had excluded the asp page from the project. After including it back into the project the formatting worked. Hope this helps.

eric lat