Is there a format code shortcut for Visual Studio?
Solution 1
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 Tools → Options → Environment → Keyboard (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 Edit → Advanced 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!)
Solution 2
Ctrl + K + D (Entire document)
Ctrl + K + F (Selection only)
Solution 3
Try Ctrl + K + D (don't lift the Ctrl key in between).
Solution 4
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 Edit → Advanced, and like all Visual Studio commands, you can set your own hotkey via menu Tools → Options → Environment → Keyboard (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.
Solution 5
ReSharper - Ctrl + Alt + F
Visual Studio 2010 - Ctrl + K, Ctrl + D
Soumya
Updated on July 08, 2022Comments
-
Soumya almost 2 years
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?
-
John Henckel over 9 yearsVS 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.
-
Peter Mortensen almost 5 yearsWhy is this tagged with 3 specific versions of Visual Studio, Visual Studio 2010, Visual Studio 2012, and Visual Studio 2013?
-
Peter Mortensen almost 4 yearsThe corresponding for Visual Studio Code is How do you format code in Visual Studio Code?
-
-
Soumya over 13 yearsMy shortcut bindings seem to differ. But the menu item location definitely helped. I need to play around with those options...
-
Jason Williams over 13 yearsIn addition, in the Tools > Options, go to Text Editor > C# > Formatting and you can control how it formats the code.
-
Soumya over 13 yearsIf 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.
-
Jpsy over 12 yearsThese 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 over 12 yearsThese are the default shortcuts for Visual Studio 2010. For VS 2005 and 2008 use the default shortcuts Ctrl+E+D and Ctrl+E+F.
-
Jason Williams over 12 yearsIndeed, but for clarity: the C# settings for vs2010 are still as described in my answer.
-
rob about 12 yearsodd, 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.
-
Grant Thomas over 10 yearsIt 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.)
-
Flash3 about 10 yearsIt also fixes the code that suddenly wraps into ONLY one line of code. I'm using VS 2012.
-
madrang almost 10 yearsIt'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... -
Jason Williams almost 10 years@madrang - you should be able to use 'format selection' to do that; you'll just have to select the line first. (Or to reduce the impact you can set up your merge/diff tools to ignore whitespace differences)
-
bugybunny over 5 yearsI just noticed that they differ. When I format the whole document (
Edit.FormatDocument
) some code cleanup also happens, e.g. I have setAdd/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 changesif(foo) bar;
toif(foo) { bar; }
. executing Edit.FormatSelection doesn’t change that. Might be a bug, gonna report it if I cannot find anything. -
bugybunny over 5 yearsOk, 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.
-
Peter Mortensen almost 4 yearsWhat version of Visual Studio is the screenshot from?
-
Upulie Han almost 4 yearsThis 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.
-
Zimano about 3 years@PeterMortensen This is from Visual Studio Code, which isn't what the OP asked about, and therefore doesn't answer the question.
-
Lance U. Matthews about 2 yearsNor is this C# code.