Skip to main content

Code Cleanup on Save in Visual Studio 2022

Earlier I posted Auto Save files in Visual Studio 2022, which saves the files in the background when Visual Studio loses the focus. There is another feature available which would do the Code Cleanup on save in Visual Studio 2022. This feature will automatically clean up your code file based on the preferences you have configured. This feature will save the developer time as he doesn’t have to deal with certain tasks related to code cleanup or coding standards. So, let’s see how can we use this new feature.

Code Cleanup on Save in Visual Studio 2022

Before you enable and start using this feature, you need to configure the code cleanup preferences. By configuring means, that there are predefined rules/preferences and out of those, one need to choose as per the need. To enable this feature, go to Tools > Options > Text Editor > Code Cleanup > Run Code Cleanup profile on Save.  (Please note, this is Visual Studio 2022 17.2.2 version.)

Code Cleanup on Save in Visual Studio 2022

Please note, while enabling this feature, make sure you select the profile from the drop-down for which you want to enable this feature.

Here, along with the option to enable Code Cleanup on Save, there is also an option to configure code cleanup. Once you click on this, you’ll see the below window. Code Cleanup is not a new feature. It is available with VS 2019 as well. You can also configure the code cleanup via Analyze > Code Cleanup > Configure Code Cleanup.

Configure Code Cleanup on Save in Visual Studio 2022

There are predefined fixer available and based on need, you can select/deselect them. You can include more fixers, selecting them from the Available fixers list and then clicking the up-arrow button. Some of the common and mostly used by the developers are format documents, Remove unnecessary imports or using, Sort imports or using, Remove unused variables etc..

Please note, Code Cleanup comes with two profiles, each customizable with different rules. You can’t define any name to these profiles and neither you can add more profiles. These profiles allow you to choose different fixer so that you can use them for different purposes. Like one for the back-end and the other for front-end code.

Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.

PS: If you found this content valuable and want to return the favour, then Buy Me A Coffee

2 thoughts to “Code Cleanup on Save in Visual Studio 2022”

Leave a Reply

Your email address will not be published. Required fields are marked *