533

In Eclipse there is a shortcut, Ctrl+Shift+F, that re-indents code and fixes comments and blank lines. Is there an equivalent for Visual Studio 2010?

4
  • VS does a subset of what Eclipse does. VS does not fix blank lines, nor reflow comments. To get the whole enchilada you need reSharper or something like that. Commented Dec 17, 2014 at 15:59
  • Why is this tagged with 3 specific versions of Visual Studio, Visual Studio 2010, Visual Studio 2012, and Visual Studio 2013? Commented Jul 20, 2019 at 10:57
  • 1
    The corresponding for Visual Studio Code is How do you format code in Visual Studio Code? Commented Jun 13, 2020 at 0:09
  • you can press Ctrl + K + F in visual studio to format code. Here are the nice explanation video on Visual Studio shortcuts youtu.be/cKULRwEWtsQ
    – Parveen
    Commented Oct 30, 2022 at 11:40

13 Answers 13

886

Visual Studio with C# key bindings

To answer the specific question, in C# you are likely to be using the C# keyboard mapping scheme, which will use these hotkeys by default:

Ctrl+E, Ctrl+D to format the entire document.

Ctrl+E, Ctrl+F to format the selection.

You can change these in menu ToolsOptionsEnvironmentKeyboard (either by selecting a different "keyboard mapping scheme", or binding individual keys to the commands "Edit.FormatDocument" and "Edit.FormatSelection").

If you have not chosen to use the C# keyboard mapping scheme, then you may find the key shortcuts are different. For example, if you are not using the C# bindings, the keys are likely to be:

Ctrl + K + D (Entire document)

Ctrl + K + F (Selection only)

To find out which key bindings apply in your copy of Visual Studio, look in menu EditAdvanced menu - the keys are displayed to the right of the menu items, so it's easy to discover what they are on your system.


(Please do not edit this answer to change the key bindings above to what your system has!)

7
  • 2
    In addition, in the Tools > Options, go to Text Editor > C# > Formatting and you can control how it formats the code. Commented Feb 9, 2011 at 7:08
  • 76
    These shortcuts (starting with Ctrl+E) are valid for Visual Studio 2005 and 2008 only. Use the shortcuts Ctrl+K+D and Ctrl+K+F to acchive the same in Visual Studio 2010 (if you are using the default configuration).
    – Jpsy
    Commented Jan 20, 2012 at 10:12
  • 3
    Indeed, but for clarity: the C# settings for vs2010 are still as described in my answer. Commented Jan 20, 2012 at 17:57
  • It also fixes the code that suddenly wraps into ONLY one line of code. I'm using VS 2012.
    – Flash3
    Commented Mar 13, 2014 at 7:56
  • It's bugs me that i couldn't find a way to format only the current line. With MonoDevelop in the options you can set Tab to auto format the current line. In VS 2013 there is no Edit.FormatLine in the Keyboard Mapping manager. When you use Git or Svn formating any other lines than the one you are working on result in a lot of useless changes...
    – madrang
    Commented Aug 31, 2014 at 17:20
130

Ctrl + K + D (Entire document)

Ctrl + K + F (Selection only)

2
  • I just noticed that they differ. When I format the whole document (Edit.FormatDocument) some code cleanup also happens, e.g. I have set Add/remove braces for single-line control statements (really bad description because the user has no idea what happens when you activate it^^) so the formatter always changes if(foo) bar; to if(foo) { bar; }. executing Edit.FormatSelection doesn’t change that. Might be a bug, gonna report it if I cannot find anything.
    – bugybunny
    Commented Oct 26, 2018 at 7:18
  • Ok, I just realized that it‘s not possible to execute all actions as the user selection can obviously be anything and not include all opening/closing parentheses etc. I still reported it because some actions should still be possible to perform.
    – bugybunny
    Commented Oct 26, 2018 at 11:43
49

Try Ctrl + K + D (don't lift the Ctrl key in between).

2
  • 3
    odd, CTRL+K+CTRL+D works (comes up in that status bar that it is recognised but CTRL+K+D does not and does nothing, might have something to do with the editor profile, but I can not remember which option I choose.
    – rob
    Commented Mar 29, 2012 at 13:44
  • 1
    This should be mentioned properly because I had no idea what ctrl+K,ctrl+D meant. I wonder why pressing 2 keys is necessary for something this basic. Commented Jul 29, 2020 at 3:18
28

Yes, you can use the two-chord hotkey (Ctrl+K, Ctrl+F if you're using the General profile) to format your selection.

Other formatting options are under menu EditAdvanced, and like all Visual Studio commands, you can set your own hotkey via menu ToolsOptionsEnvironmentKeyboard (the format selection command is called Edit.FormatSelection).

Formatting doesn't do anything with blank lines, but it will indent your code according to some rules that are usually slightly off from what you probably want.

3
  • 2
    If by "slightly off" you mean that the braces for block statements start on a new line, then it is not a problem. That is actually how I prefer it anyway.
    – user228534
    Commented Feb 9, 2011 at 7:08
  • These are the default shortcuts for Visual Studio 2010. For VS 2005 and 2008 use the default shortcuts Ctrl+E+D and Ctrl+E+F.
    – Jpsy
    Commented Jan 20, 2012 at 10:14
  • It will format the code in the way defined by your settings - the default if you don't configure them, as possible using Tools » Options » Text Editor » CSS (for VS 2012, probably something very similar in previous versions if not the same.) Commented Sep 13, 2013 at 20:11
19

ReSharper - Ctrl + Alt + F

Visual Studio 2010 - Ctrl + K, Ctrl + D

13

Format the entire document:

Ctrl + K + D

12

To align the text in the proper format -

  • Ctrl + K + D for front end pages like .aspx or .cshtml

  • Ctrl + K + F for a .cs page

But observe to press all buttons in sequence...

11

Simply

For Visual Studio Code Use ALt + Shift + F

for Visual Studio IDE Press Ctrl + K followed by Ctrl + D


It will beautify/format your entire file.

1
  • when the paying version is worse than the free one...
    – Mehdi
    Commented Jul 14, 2023 at 14:12
9

Most easy, try once

  1. Select all codes using :- Ctrl + A
  2. Press :- Ctrl + K + D
1
  • True, but why add the same answer again?
    – Sam Jones
    Commented Sep 8, 2022 at 10:57
5

Select all text in the document and press Ctrl + E + D.

4

Right click on the code, and you have "Format Document". In my case it is Ctrl+Shift+I

enter image description here

3
  • What version of Visual Studio is the screenshot from? Commented Jun 12, 2020 at 10:14
  • 1
    @PeterMortensen This is from Visual Studio Code, which isn't what the OP asked about, and therefore doesn't answer the question.
    – Zimano
    Commented Jun 1, 2021 at 8:47
  • Nor is this C# code. Commented May 24, 2022 at 8:33
2

Change these shortcuts in Visual Studio

Tools → Options → Environment → Keyboard

and then change the command

"Edit.FormatDocument" or "Edit.FormatSelection"

assign the same shortcut alt + shift +f as in visual studio code in order to not remember another one and confuse between each other.

2

In Visual Studio 2022, it's Ctrl + K followed by Ctrl + D.