Visual-studio – How to auto format code in Visual Studio

indentationvisual studio

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

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