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

31

Best Answer


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

Visual Studio's formatting code feature is a powerful tool that helps improve code readability and consistency. By automatically formatting your code according to predefined rules, you can ensure that your codebase is consistent and easier to read.

One of the key benefits of formatting code in Visual Studio is that it helps make your code easier to maintain. When all the code in a project follows the same formatting rules, it becomes easier for developers to understand and modify the code. This can save a significant amount of time and effort in the long run.

Formatting code in Visual Studio is simple. Just go to the Edit menu, select Advanced, and then choose Format Document. Visual Studio will automatically format your code according to the rules specified in the code style settings.

Additionally, Visual Studio allows you to customize the formatting rules according to your specific requirements. You can modify the indentation, line spacing, and other formatting options to match your preferred coding style.

In conclusion, Visual Studio's formatting code feature is an indispensable tool for any developer. By automatically formatting your code, you can improve its readability, consistency, and maintainability, ultimately saving time and effort in the development process.

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

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

  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

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

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

You can define new key bindings by going to ToolsOptionsEnvironmentkeyboard:

Enter image description here

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.

On mac : Shift + Option + F

On ubuntu : Ctrl + Shift + I

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

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

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

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

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

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

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.

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

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.

Right click:

Enter image description here

It works in Visual Studio 2015, maybe earlier version.

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.

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

See this image

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. Windows Shift + Alt + F
  2. MacOS Shift + Option + F
  3. Linux Ctrl + Shift + I

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

Visual Studio Extension - CodeMaid is pretty fine.

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

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.

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.

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

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.