Starting from VS2022-Preview2, there is no need to additional extension to "format on-save", you just run a customized code cleanup that contains "Format document" action on-save:
- Customize your cleanup profile to perform "Format document" action (if not already there), in
Analyze > Code Cleanup > Configure Code Cleanup - Enable "Run code cleanup profile on save" in VS configuration
Tools > Options > Text Editor > Code Cleanup
Bringing Code Cleanup on Save To Visual Studio 2022 17.1 Preview 2
Unfortunately code cleanup setting is not included to .editorconfig file neither to exported config file .vssettings that could be shared across a team, maybe it will be included in the future.
Some feature requests related to this:
visual studio 2019 allow import and export Configure Code Cleanup profile
Provide a way to share Code Cleanup profiles as related to EditorConfig
Code Cleanup and EditorConfig
Starting from VS2022-Preview2, there is no need to additional extension to "format on-save", you just run a customized code cleanup that contains "Format document" action on-save:
- Customize your cleanup profile to perform "Format document" action (if not already there), in
Analyze > Code Cleanup > Configure Code Cleanup - Enable "Run code cleanup profile on save" in VS configuration
Tools > Options > Text Editor > Code Cleanup
Bringing Code Cleanup on Save To Visual Studio 2022 17.1 Preview 2
Unfortunately code cleanup setting is not included to .editorconfig file neither to exported config file .vssettings that could be shared across a team, maybe it will be included in the future.
Some feature requests related to this:
visual studio 2019 allow import and export Configure Code Cleanup profile
Provide a way to share Code Cleanup profiles as related to EditorConfig
Code Cleanup and EditorConfig
"Format Document On Save" worked for me.
Configuration:

I have
Ctrl + K, Ctrl + F
to format the selected text
or
Ctrl+ K, + Ctrl + D for the document :
You may of used Format Document on Save Visual Studio extension.
Enables auto formatting of the code when you save a file. Visual Studio supports auto formatting of the code with the CTRL+E,D or CTRL+E,F key shortcuts but with this extension the command 'Format Document' is executed on Save.
Videos
Hi everyone,
I'm really struggling to have proper code formatting in my Visual studio 2022 and would love to hear your guys advice, tricks or experiences:
Let me start with what options I'm aware of:
1. Tools -> options -> C# -> Code Style -> Formatting
I like this option but it simply does not cover all scenarios I want formatted. For example removing white lines in my methods. This is an option that seems so obvious but there is not a checkmark to configure it (as far as I can see).
2. Shortcuts: Tools -> options -> Environment -> Keyboard -> EditFormatDocument
This is great that I'm able to have a shortcut, I have it on visual studio 6 as mapping scheme. But I found that it doesn't always work and for example sometimes I need to remove all indents on a method and then select it + format. Only then does the formatting work. Also here it doesn't remove white spaces.
3. On save: Tools -> options -> Text Editor -> Code Cleanup
Here I know I am able to have some configurations done when clicked on save. Also here a white space remover seems not to be there.
Overall I just find the Formatting options and keyboard shortcut options very limited. An option in VSCode for example is to copy your current line to a line below, I havent found this option yet in Visual Studio.
I love the Visual Studio for its IDE and for example test explorer, but formatting is such a huge thing that I feel like I'm missing something or Visual Studio needs to step up there game.
Would love what you guys think and I really hope that I am overlooking some functionality or something :)
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
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 -> Format Document (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
Hi! I was wondering if there's a way to format the code automatically. VS has it's own formatter but it doesn't add semicolon automatically and it doesn't format empty space well enough. In VS code for example, when I format it formats everything well.
Couldn't find an extension for VS like prettier or beautify.
How can I automate this?
hi some days ago I found an extension named "continuous formatting" the point of this extension is to format code as you type. the downside is it is not free, does anyone know similar extensions that are free? I know some extensions are there that format code when you save the file but I don't want them.
thanks.