How do you auto format code in Visual Studio?
Visual StudioIndentationVisual Studio Problem Overview
I know Visual Studio can auto format to make my methods and loops indented properly, but I cannot find the setting.
Visual Studio Solutions
Solution 1 - Visual Studio
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
Solution 2 - Visual Studio
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
Solution 3 - Visual Studio
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":
Solution 4 - Visual Studio
-
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.
Solution 5 - Visual Studio
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:
Solution 6 - Visual Studio
You can define new key bindings by going to Tools → Options → Environment → keyboard:
Solution 7 - Visual Studio
On mac : Shift + Option + F
On ubuntu : Ctrl + Shift + I
Solution 8 - Visual Studio
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
Solution 9 - Visual Studio
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
Solution 10 - Visual Studio
I used to use these combinations. I automated this process on Save of a document. You can try my extension Format Document on Save.
Solution 11 - Visual Studio
If you display the HTML Source Editing toolbar, there is a "Format the Whole Document" button as well.
Solution 12 - Visual Studio
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
Solution 13 - Visual Studio
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
Solution 14 - Visual Studio
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.
Solution 15 - Visual Studio
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
https://i.stack.imgur.com/oYkDp.png" />
Solution 16 - Visual Studio
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.
Solution 17 - Visual Studio
Solution 18 - Visual Studio
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.
Solution 19 - Visual Studio
In newer versions, the shortcut for the document-wide formatting is: Shift + Alt + F
Solution 20 - Visual Studio
Select the data and the right click and you will find this option. Format Document and Format Selection:
Solution 21 - Visual Studio
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
.
Solution 22 - Visual Studio
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)
Solution 23 - Visual Studio
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.
Solution 24 - Visual Studio
With the Continuous Formatting extension (commercial, developed by me), the code is formatted really automatically as you type.
Solution 25 - Visual Studio
Visual Studio Extension - CodeMaid is pretty fine.
Solution 26 - Visual Studio
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.
Solution 27 - Visual Studio
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.
Solution 28 - Visual Studio
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. :)
Solution 29 - Visual Studio
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:
Solution 30 - Visual Studio
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
Solution 31 - Visual Studio
In Windows, use the following:
Keyboard shortcuts Ctrl + E, D
or
Menu Edit → Advanced → Format Document
Solution 32 - Visual Studio
If you are using JetBrains ReSharper, the easiest way to format your code is to use the CTRL + ALT + ENTER shortcut (Reformat code).
Solution 33 - Visual Studio
You can press shift + alt + F.
Solution 34 - Visual Studio
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.
Solution 35 - Visual Studio
Cut/Paste of a section is another quick way (and easy to remember).
Solution 36 - Visual Studio
#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);
}
Solution 37 - Visual Studio
Simply hitting Ctrl+Shift+I will do the job.