I know Visual Studio can auto format to make my methods and loops indented properly, but I cannot find the setting.
-
47From top menu: Edit --> Advanced --> Format Document – zipzit Feb 06 '17 at 21:47
-
9what about for 2019? – Alex Gordon Apr 02 '19 at 17:15
-
3still the same in vs2019 – Finn May 27 '19 at 08:05
-
1Do you mean "how do you format a block of code with a couple keystrokes"? That seems to be what the popular answers refer to (rather than "how do you auto format" ... as you type/paste/etc). – KevinVictor May 31 '19 at 15:53
-
1For Visual Studio 2017 . Using shortcut Ctrl + Alt + Enter – Fairuz Sulaiman Oct 15 '19 at 03:00
-
1This feature will be available in Visual Studio 2022 17.1 - see: https://stackoverflow.com/a/70740018/11473934 – Kraego Jan 17 '22 at 10:48
-
You can use `ideal` formatting if you choose Tools -> Text Editor -> Advanced -> Text formatting method -> Ideal. It's much better than default formatting. Then just use ctrl + k and ctrl + d – Mikhail Ionkin Feb 10 '23 at 09:21
-
I want to add an icon that does this to the toolbar, know where i can find it? – niico Jul 29 '23 at 20:57
38 Answers
To format a selection: Ctrl+K, Ctrl+F
To format a document: Ctrl+K, Ctrl+D
See the pre-defined keyboard shortcuts. (These two are Edit.FormatSelection
and Edit.FormatDocument
.)
Note for macOS
On macOS, use the CMD ⌘ key instead of Ctrl:
- To format a selection: CMD ⌘+K, CMD ⌘+F
- To format a document: CMD ⌘+K, CMD ⌘+D

- 9,608
- 6
- 34
- 56

- 25,686
- 13
- 66
- 119
-
20
-
33
-
6In VS2010, if you have `Format document is not available...` error message, please read: http://stackoverflow.com/q/8812741/1016891 – tom_mai78101 Sep 10 '14 at 03:47
-
4
-
2
-
For the sake of posterity, you might consider updating this answer with the default keybindings for older versions of VS. I recognize the comments have the answers, but that's hardly a long-term solution. – FuriousFolder Aug 04 '15 at 13:43
-
Difference between `Ctrl+K` and `Ctrl+E`: http://stackoverflow.com/questions/4942113/format-code-shortcut-for-visual-studio – JNF Jan 06 '16 at 07:36
-
-
-
14I dont know why Microsoft keeps changing these. For future reference, it can be found under Edit - > Advanced -> Format Document Who knows, they're probably going to rename that menu in the next iteration. – Jeremy Jun 12 '17 at 07:23
-
Using VS 2017, Found it under Edit>Advanced>Format Document, but it doesn't form the .cshtml file. – xpagesbeast Jan 10 '19 at 06:20
-
Now-a-days it is `Shift + Alt + F` in windows, or you can find it in Command Palette ( `Ctrl + Shift + P' ) and type **Format Document** – Akash Nov 01 '20 at 10:20
-
-
-
If you using Visual Studio 2022 17.1 - see: https://stackoverflow.com/a/70740018/11473934 – Kraego Jan 17 '22 at 12:16
-
1In the menu Edit, there is no option "Advanced". ctrl+k, ctrl+d has no effect. ctrl+k, ctrl+f says "is not a command". – user0 Jan 24 '22 at 13:20
For Visual Studio 2010/2013/2015/2017/2019
- Format Document (Ctrl+K,Ctrl+D), i.e. press&hold Ctrl, press&release K then tap D as it is a sequence
- Format Selection (Ctrl+K,Ctrl+F)
Toolbar Edit -> Advanced (If you can't see Advanced, select a code file in solution explorer and try again)
Your shortcuts might display differently to mine as I am set up for C# coding but navigating via the toolbar will get you to your ones.
If it isn't working, look for errors in your code, like missing brackets which stop auto format from working

- 4,967
- 2
- 33
- 42
-
11If you find that it isn't working, check your brackets {}, (), when it doesn't work for me, it is because there are open brackets somewhere in the file. – ono2012 Oct 17 '12 at 12:35
-
Is there any way to not use a sequence of keys but just on combination of keys? – Upulie Han Jul 15 '20 at 17:43
-
1@UpulieHan I think you can edit your shortcuts Tools > Options > Environment > Keyboard > Search for "Edit.FormatDocument" ... Ctrl+Alt+; seems like an available combo for me :) – ono2012 Jul 17 '20 at 11:13
-
If you are trying to auto format HTML code then this command won't work, instead look at https://stackoverflow.com/questions/41329393/how-to-format-html-in-visual-studio-code – Gianluca Veschi Apr 11 '21 at 15:44
I have installed an extension named "Format document on Save" which formats the whole document every time you save it.
For installing it in Visual Studio 2015 or Visual Studio 2017, on Tools just click the "Extensions and Updates...":
And then just go to "Online" at the left panel and search for "Format document on save":

- 30,738
- 21
- 105
- 131

- 961
- 1
- 9
- 14
-
1Also you can install it by visual studio market place https://marketplace.visualstudio.com/items?itemName=mynkow.FormatdocumentonSave – VMM Mar 28 '17 at 07:29
-
I tried it, but it looks like [this issue](https://github.com/dotnet/roslyn/issues/29482) is getting in the way. And I don't want to clean up what VS messed up while doing a cleanup. – S. Kalabukha Nov 12 '21 at 09:32
Go to menu Tools → Extensions & Updates and type "productivity" in search:
Install 'Productivity Power Tools 2015'
Restart Visual Studio.
Go to menu Tools → Options → Productivity Power Tools → Power Commands and check "Format document on save":
Note: In VS2022 we don't have power commands.
- If anyone want to have "Format document on save" and "Remove and sort using on save" install Mads Kristensen extension for VS2022 https://marketplace.visualstudio.com/items?itemName=MadsKristensen.CodeCleanupOnSave
- After installing this extension it will automatically "Format document on save", "Remove and sort using on save" and "Apply file header preferences".
- If we want to customize default settings click on "Configure Code Cleanup" menu item to add/remove any available fixers.

- 629
- 6
- 9

- 451
- 4
- 5
-
-
have to get used to formatting every new document you edit and committing changes before you actually change the file otherwise the commits can look pretty noisy – smurtagh Apr 10 '18 at 18:53
-
Installed the extension for visual studio 2019, i don'tsee powerCommands after HTML Copy. – Amir Dora. May 28 '20 at 04:45
-
It works for me, Microsoft Visual Studio Community 2019 Version 16.5.2 – Slava Latun May 29 '20 at 08:36
Visual Studio 2019 & 2022
- Format Document, While you're holding down Ctrl button, first press K then D
- Format Selection, While you're holding down Ctrl button, first press K then F
or just click Edit => Advanced => Format Document / Format Selection

- 3,348
- 3
- 31
- 31
Follow the steps below:
- Go to menu Tools
- Go to Options
- Go to the Text Editor options
- Click the language of your choice. I used C# as an example.
See the below image:

- 30,738
- 21
- 105
- 131

- 10,651
- 9
- 52
- 74
-
34And once I've done this? I can't see any of those settings handle automatic indentation? – MrJalapeno Jun 20 '17 at 09:17
-
37@MrJalapeno I have absolutely no idea what question I was answering here 6 years ago but 34 people have found it useful. Sorry for the confusion. I probably misinterpreted the question in which case my answer applies since there are formatting related options in my answer. In hindsight, it's safe to say the OP was looking for ctrl+k, ctrl+d. – Pete Dec 07 '17 at 03:16
You can define new key bindings by going to Tools → Options → Environment → keyboard:

- 30,738
- 21
- 105
- 131

- 3,926
- 1
- 29
- 42
SinceVisual Studio 2022 17.1
there is a builtin Feature to run code formatting on save (see devblogs.microsoft), meaning there is no need to install extensions like Format document on Save
.
Be aware that there is a failure regarding the merge view when using the autoformat option - github code cleanup - merge bug.

- 2,978
- 2
- 22
- 34
On mac : Shift + Option + F
On ubuntu : Ctrl + Shift + I

- 576
- 4
- 14
-
2This, unlike basically everything else here, still worked for me in 2021 (Ubuntu). – Kotlopou Aug 23 '21 at 08:21
-
1
In Visual Studio 2017, 2019, 2022
Format Document is Ctrl + E, D.
But...if you want to add the Format Document button to a tool bar do this:
- Right click on tool bar.
- Select "Customize.."
- Select the "Commands" Tab.
- Select the "Toolbar" radio button.
- Select "Text Editor" from the pull down next to the radio button (or whatever tool bar you want the button on)
Now...
- Click the Add Command button.
- Categories: Edit
- Commands: Document Format
- Click OK

- 5,040
- 16
- 42
- 49
I used to use these combinations. I automated this process on Save of a document. You can try my extension Format Document on Save.

- 30,738
- 21
- 105
- 131

- 4,408
- 4
- 38
- 65
-
1Thanks for doing something that should just have been a checkbox in VS settings! – sirclesam Apr 30 '21 at 18:21
If you display the HTML Source Editing toolbar, there is a "Format the Whole Document" button as well.

- 30,738
- 21
- 105
- 131

- 584
- 2
- 13
- 29
-
That's a really strange place for it. I have been searching all over for this. – rory.ap Jan 07 '15 at 15:29
-
2I have VS13, that toolbar does not have a "Format Document" button available. – emcor Feb 12 '15 at 01:03
-
2Using Microsoft Visual Studio Community 2015 Version 14.0.23107.0 D14REL there is a "Format the whole document" button on the toolbar mentioned. – Ian Carpenter Jul 27 '15 at 13:39
-
Thank you. It would be nice if we could add it to other toolbars by **add or remove buttons>>customize** – Eftekhari Jul 25 '20 at 11:40
The solution provided in accepted answer does not apply to Microsoft Visual Studio 2012.
In case of Visual Studio 2012, the shortcuts are:
- For a highlighted block of code: Ctrl + K, Ctrl + F
- For the document-wide formatting: Ctrl + K, Ctrl + D

- 30,738
- 21
- 105
- 131

- 624
- 1
- 12
- 36
-
Actually it's not wrong. Both CTRL + K,F and CTRL + E,F do the exact same thing. If you go to EDIT -> Advanced, you will actually see the shortcuts listed as CTRL + E,D and CTRL + E,F. Also commenting can be done with either CTRL + K,C or CTRL + E,C. - Using VS2012 Premium with all the latest updates installed. – Alan006 Mar 25 '14 at 16:49
-
1@Alan006 Interesting. My installation of VS2012 Pro says Ctrl+ e,f/Ctrl + e,d are not commands. – Geowil May 04 '14 at 19:36
-
5I think it might be to do with what environment/development settings you choose initially on first launch. They both work for me though, so I wont complain :D – Alan006 May 04 '14 at 19:41
-
@Alan006 is right, the default environment for VS2010 and up uses the keyboard layout for "Visual Basic" (check **Tools -> Options -> Keyboard**). "Visual C# 2005" has slightly different keyboard bindings. – Bryan Way Jul 17 '15 at 22:01
In Visual Studio 2019 , "Code Cleanup" (RunDefaultCodeCleanup) is more advanced (taken from ReSharper): Ctrl + K, Ctrl + E
Options dialog box: Text Editor → C# → Code Style → Formatting
Auto formatting settings in Visual Studio


- 30,738
- 21
- 105
- 131

- 447
- 7
- 8
Select the text you want to automatically indent.
Click menu Edit → Advanced → *Format Selection, or press Ctrl + K, Ctrl + F. Format Selection applies the smart indenting rules for the language in which you are programming to the selected text.
Step (1): Ctrl + A
Step (2): Ctrl + K
Step (3): Ctrl + F

- 30,738
- 21
- 105
- 131

- 10,807
- 1
- 75
- 53
Under menu Tools → Options → Text Editor, then going to the Formatting → General section of whatever language you wish to format you will find General. Check all three formatting check-boxes.
Under menuTools → Options → Text Editor, then going to the TABS section of whatever language you wish to format you will find Indenting. Select Smart and it will activate automatic formatting whenever you use one of the closing elements ; ) } within that block.
There isn't any need for keystrokes.

- 30,738
- 21
- 105
- 131

- 389
- 5
- 8
In newer versions, the shortcut for the document-wide formatting is: Shift + Alt + F

- 1,179
- 11
- 17
You can add the buttons to your toolbar by clicking the little drop down arrow to the right of the last toolbar button, select "Add or Remove Buttons" and then click the buttons you want to add a tick to them. The button(s) you select will appear on your toolbar ...
Then you just select text and click the Increase Indent or Decrease Indent buttons. I tested this on Visual Studio 2013 only.

- 541
- 8
- 23
The original question said "I cannot find the setting."
Simple answer is: Look at top menu, then
Edit --> Advanced --> Format Document
You will also see the currently assigned key strokes for that function. Nothing special to memorize. This really helps if you use multiple developer environments on different operating systems.

- 3,778
- 4
- 35
- 63
Select the data and the right click and you will find this option. Format Document and Format Selection:

- 30,738
- 21
- 105
- 131

- 57
- 1
- 1
-
1This thread is about Visual Studio. Your comment talks about Visual Studio Code – S. ten Brinke Nov 15 '19 at 09:20
- Windows Shift + Alt + F
- MacOS Shift + Option + F
- Linux Ctrl + Shift + I

- 29,388
- 11
- 94
- 103

- 491
- 4
- 9
Just to further Starwfanatic and Ewan's answers, you can customise your IDE to add any button to any toolbar - so you can add the Format button (as the HTML Source Editing toolbar has) to any other toolbar (like Text Editing with all the other edit controls like increase/decrease indent).
Click the arrow to the right of the toolbar → Add or Remove Buttons → Customize... → Commands tab → button.
Document Format and Selection Format are both under the Edit group.
(Tested in Visual Studio 2010 and Visual Studio 2013)

- 30,738
- 21
- 105
- 131

- 113
- 1
- 7
With the Continuous Formatting extension (commercial, developed by me), the code is formatted really automatically as you type.

- 26,641
- 3
- 64
- 66
-
That's great and should be included in VS by default, but the price is too high. For 19 € I would have said "Great!". – tmighty Dec 01 '17 at 12:44
In Visual Studio 2015 and 2017 for C# code.
- Scroll to the end of the file
- Remove the last "curly bracket", }
- Wait until the line above it shows an error
- Replace the "curly bracket", } Fini. :)

- 30,738
- 21
- 105
- 131

- 49
- 8
Visual Studio Extension - CodeMaid is pretty fine.

- 3,213
- 3
- 40
- 59

- 123
- 1
- 11
-
1
-
1With this extension, you can even right click a folder and cleanup all code in all files in it, very convenient. – xinthose May 06 '20 at 15:55
If it's still not working then you can select your entire document, copy and paste and it will reformat.
So ...
- Ctrl + A
- Ctrl + C
- Ctrl + V
This is the only thing that I have found that works in Visual Studio Community Edition on Mac.

- 30,738
- 21
- 105
- 131

- 2,004
- 3
- 19
- 30
It's Shift + Alt + F in Windows.
But you can always double check by doing following.
- Go to menu View → Command Palette or press Ctrl + Shift + P
- Search for Format Document
- You can press that to Auto Format Code or its hot key, written in front of it.

- 30,738
- 21
- 105
- 131

- 139
- 1
- 10
-
Command pallet doesn't exist in Visual Studio. You must be mistaking this with vscode. – TamusJRoyce Nov 18 '22 at 18:17
If you can afford it (or if you're eligible for the 30-day free trial) JetBrains' ReSharper can reformat a whole project directory.
Just install → right-click a directory → select Cleanup Code from the context menu.

- 30,738
- 21
- 105
- 131

- 1,011
- 11
- 15
You can also try the right click menu (context menu) option to format the selection of the coding document. Take a look at the below screenshot:

- 30,738
- 21
- 105
- 131

- 26
- 4
You can also use the CodeMaid Extension.
You can get the extension from Visual Studio Market Place.
Here is the link to the extension. CodeMaid
-
1Can you elaborate? Please respond by [editing your answer](https://stackoverflow.com/posts/59460717/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Jan 13 '21 at 21:52
In Windows, use the following:
Keyboard shortcuts Ctrl + E, D
or
Menu Edit → Advanced → Format Document

- 30,738
- 21
- 105
- 131

- 1,867
- 1
- 7
- 16
If you are using JetBrains ReSharper, the easiest way to format your code is to use the CTRL + ALT + ENTER shortcut (Reformat code).

- 30,738
- 21
- 105
- 131

- 46
- 5
Using VS 2017 I wanted to format web page HTML which somehow had become entirely left aligned. Even after installing Productivity Power Tools this wouldn't work.
To fix, for HTML I had to go to Tools --> Options --> Text Editor --> HTML (Web Forms) and change; Tabs Indenting = Smart and Tab = Keep Tabs.

- 1
Cut/Paste of a section is another quick way (and easy to remember).

- 319
- 3
- 3
-
3...? that does not format anything for me. in fact, the reason I looked this up was to format AFTER cutting and pasting.. – tenwest Mar 25 '16 at 22:09
-
1That does not format your code with the defatult settings. Maybe you have some setting / plugin that does the job after pasting. Up to Novermber 2018 (v 1.30.2) the way is SHIFT + ALT + F – Pablo Jan 10 '19 at 14:01
#include "stdafx.h"
#include <stdio.h>
int main(){
char filename[15];
printf("Enter file name:");
scanf("%s", filename);
Automat(filename);
freq(filename);
}
int Automat(char filename[]){
FILE*fp;
char c;
int state = 1;
int duma = 0;
fp = fopen(filename, "r");
if (fp == NULL){
printf("Error");
system("pause");
}
while (!feof(fp)){
c = fgetc(fp);
switch (state){
case 1:
if (c == '\t' || c == '\n' || c == ' '){
state = 3;
break;
}
case 2:
if (c >='A'|| c<='Z'){
break;
}
case 3: duma++;
state = 1;
break;
}
}
printf("Broq e : %d\n", duma);
return 1;
}
int freq(char filename[]){
FILE*fp;
char c;
int state = 1, sequence = 0;
fp = fopen(filename, "r");
if (fp == NULL){
printf("Error");
system("pause");
}
while (!feof(fp)){
c = fgetc(fp);
switch (state){
case 1:
if (c >= 'A' && c <= 'Z'){
state = 2;
break;
}
case 2:
if (c == '\t' || c == '\n'||c == ' '){
sequence++;
state = 1;
break;
}
}
}
printf("Sequence is : %d\n", sequence);
}

- 11,977
- 56
- 49
- 78

- 1
-
1The question was *"How do you auto format code in Visual Studio?"*. How does this answer address that? – Peter Mortensen Mar 20 '21 at 19:09