In previous article, I mentioned Convert C# Class to JSON with Example but in this article, I have mentioned how we can format code in Visual Studio and how we can also auto format code (with indentation) on save in Visual Studio.

Format Visual Studio Shortcut with Indentation

Let's take a look at various ways using which you can format code or current open file using Visual Studio

Using Shortcut

For Visual Studio 2010/2013/2015/2017/2019/2022

You can simply use CTRL +K then CTRL+D to format current document in Windows


  • On Windows: CTRL +K , CTRL+D
  • On Mac: Shift + Option + F
  • On Ubuntu(Linux): Ctrl + Shift + I

Note: if there is opening bracket (), {} but not closing bracket then above shortcut will not work.

If you want to change Visual Studio shortcut, you can define new key bindings by going to Tools -> Options -> Environment -> keyboard.

Using Menu

Using Visual Studio, you can click on Edit -> Advanced -> Format Document

Auto Format Visual Studio Document on Save

If you want to auto-format code on save in Visual Studio, then you will have to download a Extention for it which is Code Cleanup, it can be used with Visual Studio 2019 and Visual Studio 2022

Code Cleanup is a new extention of Visual Studio, that will automatically clean up your code file to make sure it is formatted correctly and that your coding style preferences are applied.

This extension will perfor the Code Cleanup automatically when the file is being saved.

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

For Older version of Visual Studio (VS 2017, or VS 2015), instead of using Code Cleanup, you can use other extensions like

  • Productivity Power Tools 2015
  • Format document on save

To Install a Extension in Visual Studio, follow these steps:

  • Go to menu Tools -> Extensions & Updates and type "productivity" in search
  • Install 'Productivity Power Tools 2015'
  • Then Restart Visual Studio to make it work.

Hope this helps.

You may also like to read:

Useful Visual Studio Shortcuts

Difference between Visual Studio Build, Re-build and Clean, solution

How to comment multiple lines in Visual Studio (VS) Code

What is .NET framework? & Understanding .NET framework architecture

Create GUID in C# (Various examples)