How do you auto format code in 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

Leave a Comment