1156

I know Visual Studio can auto format to make my methods and loops indented properly, but I cannot find the setting.

8
  • 47
    From top menu: Edit --> Advanced --> Format Document
    – zipzit
    Feb 6, 2017 at 21:47
  • 9
    what about for 2019? Apr 2, 2019 at 17:15
  • 3
    still the same in vs2019
    – Finn
    May 27, 2019 at 8:05
  • 1
    Do 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). May 31, 2019 at 15:53
  • 1
    For Visual Studio 2017 . Using shortcut Ctrl + Alt + Enter Oct 15, 2019 at 3:00

38 Answers 38

1743

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
16
  • 20
    vs 2010 seems to be ctrl + k, ctrl + d May 8, 2014 at 9:50
  • 33
    Its Ctr + K, Ctrl + D in Visual Studio 2013 to format a document.
    – Kris
    Aug 21, 2014 at 14:49
  • 6
    In VS2010, if you have Format document is not available... error message, please read: stackoverflow.com/q/8812741/1016891 Sep 10, 2014 at 3:47
  • 4
    These are accurate for VS2013 Apr 28, 2015 at 17:01
  • 14
    I 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, 2017 at 7:23
199

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
  • 11
    If 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, 2012 at 12:35
  • Is there any way to not use a sequence of keys but just on combination of keys? Jul 15, 2020 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, 2020 at 11:13
  • If you are trying to auto format HTML code then this command won't work, instead look at stackoverflow.com/questions/41329393/… Apr 11, 2021 at 15:44
65

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...":

Enter image description here

And then just go to "Online" at the left panel and search for "Format document on save":

Enter image description here

2
  • 1
    Also you can install it by visual studio market place marketplace.visualstudio.com/…
    – VMM
    Mar 28, 2017 at 7:29
  • I tried it, but it looks like this issue is getting in the way. And I don't want to clean up what VS messed up while doing a cleanup. Nov 12, 2021 at 9:32
47

Visual Studio 2019 & 2022

  1. Format Document, While you're holding down Ctrl button, first press K then D
  2. Format Selection, While you're holding down Ctrl button, first press K then F

or just click Edit => Advanced => Format Document / Format Selection

enter image description here

0
45
  1. Go to menu ToolsExtensions & Updates and type "productivity" in search:

    1

  2. Install 'Productivity Power Tools 2015'

  3. Restart Visual Studio.

  4. Go to menu ToolsOptionsProductivity Power ToolsPower Commands and check "Format document on save":

    2

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. enter image description here
4
  • Would have been good if it working while typing, as with VB.NET:
    – tmighty
    Dec 1, 2017 at 12:41
  • 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, 2018 at 18:53
  • Installed the extension for visual studio 2019, i don'tsee powerCommands after HTML Copy.
    – Amir Dora.
    May 28, 2020 at 4:45
  • It works for me, Microsoft Visual Studio Community 2019 Version 16.5.2 May 29, 2020 at 8:36
41

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:

Enter image description here

2
  • 34
    And once I've done this? I can't see any of those settings handle automatic indentation?
    – MrJalapeno
    Jun 20, 2017 at 9: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 7, 2017 at 3:16
31

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.

enter image description here

Be aware that there is a failure regarding the merge view when using the autoformat option - github code cleanup - merge bug.

29

You can define new key bindings by going to ToolsOptionsEnvironmentkeyboard:

Enter image description here

1
  • 1
    Thanks. For me Ctrl E + D in VS2005 scheme :)
    – nawfal
    Nov 17, 2016 at 10:30
23

On mac : Shift + Option + F

On ubuntu : Ctrl + Shift + I

2
  • 2
    This, unlike basically everything else here, still worked for me in 2021 (Ubuntu).
    – Kotlopou
    Aug 23, 2021 at 8:21
  • 1
    I'm glad to help =) Aug 30, 2021 at 19:35
14

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
0
12

I used to use these combinations. I automated this process on Save of a document. You can try my extension Format Document on Save.

1
  • 1
    Thanks for doing something that should just have been a checkbox in VS settings!
    – sirclesam
    Apr 30, 2021 at 18:21
11

If you display the HTML Source Editing toolbar, there is a "Format the Whole Document" button as well.

4
  • That's a really strange place for it. I have been searching all over for this.
    – rory.ap
    Jan 7, 2015 at 15:29
  • 2
    I have VS13, that toolbar does not have a "Format Document" button available.
    – emcor
    Feb 12, 2015 at 1:03
  • 2
    Using Microsoft Visual Studio Community 2015 Version 14.0.23107.0 D14REL there is a "Format the whole document" button on the toolbar mentioned. Jul 27, 2015 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, 2020 at 11:40
10

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
4
  • 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, 2014 at 16:49
  • 1
    @Alan006 Interesting. My installation of VS2012 Pro says Ctrl+ e,f/Ctrl + e,d are not commands.
    – Geowil
    May 4, 2014 at 19:36
  • 5
    I 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 4, 2014 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, 2015 at 22:01
9

In Visual Studio 2019 , "Code Cleanup" (RunDefaultCodeCleanup) is more advanced (taken from ReSharper): Ctrl + K, Ctrl + E

Options dialog box: Text EditorC#Code StyleFormatting

Auto formatting settings in Visual Studio

8

Select the text you want to automatically indent.

Click menu EditAdvanced → *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

7

Under menu ToolsOptionsText Editor, then going to the FormattingGeneral section of whatever language you wish to format you will find General. Check all three formatting check-boxes.

Under menuToolsOptionsText 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.

1
  • 1
    This doesn't improve "already messed up" code while VB.NET does this.
    – tmighty
    Dec 1, 2017 at 12:40
6

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

5

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 ...

enter image description here

Then you just select text and click the Increase Indent or Decrease Indent buttons. I tested this on Visual Studio 2013 only.

5

Right click:

Enter image description here

It works in Visual Studio 2015, maybe earlier version.

5

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.

4

Select the data and the right click and you will find this option. Format Document and Format Selection:

See this image

1
  • 1
    This thread is about Visual Studio. Your comment talks about Visual Studio Code Nov 15, 2019 at 9:20
2

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 ButtonsCustomize... → Commands tab → button.

Document Format and Selection Format are both under the Edit group.

(Tested in Visual Studio 2010 and Visual Studio 2013)

1

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

1
  • 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 1, 2017 at 12:44
1

Visual Studio Extension - CodeMaid is pretty fine.

2
  • 1
    Extension not found
    – Sushan
    Apr 20, 2020 at 11:41
  • 1
    With this extension, you can even right click a folder and cleanup all code in all files in it, very convenient.
    – xinthose
    May 6, 2020 at 15:55
1

In Visual Studio 2015 and 2017 for C# code.

  1. Scroll to the end of the file
  2. Remove the last "curly bracket", }
  3. Wait until the line above it shows an error
  4. Replace the "curly bracket", } Fini. :)
1
1

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.

1

It's Shift + Alt + F in Windows.

But you can always double check by doing following.

  1. Go to menu ViewCommand Palette or press Ctrl + Shift + P
  2. Search for Format Document
  3. You can press that to Auto Format Code or its hot key, written in front of it.
1
  • Command pallet doesn't exist in Visual Studio. You must be mistaking this with vscode. Nov 18, 2022 at 18:17
1
  1. Windows Shift + Alt + F
  2. MacOS Shift + Option + F
  3. Linux Ctrl + Shift + I
1
  • 6
    vscode != visual studio Nov 18, 2022 at 18:14
0

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

1
  • 1
    Can you elaborate? Please respond by editing your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today). Jan 13, 2021 at 21:52
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.