Enforce consistent coding styles across a team using EditorConfig files in Visual Studio

In Visual Studio if you want to enforce consistent coding styles in a project or solution check out EditorConfig files. An EditorConfig file can be checked into source control and it overrides local VS settingsĀ which means all developers don’t need to change their settings to align.. AWESOME!!!

… and since VS 2019 16.10 EditorConfig files now have a user interface so we don’t need to edit the files directly.

To run the formatting options you need to format document via Edit-> Advanced -> Format Document or click Ctrl+K,Ctrl+D or by running code cleanup.

An example of what the interface looks like is below.

Run code cleanup in Visual Studio to apply ‘fixers’ and tidy ups to your code

It’s not the most obvious function in Visual Studio but the run code cleanup button located at the bottom of our code files is very helpful.

It allows us to apply a number of ‘fixers’ to the file such as format document (as per EditorConfig file), sort usings, remove unnecessary usings and more together so we don’t have to run individual keyboard shortcuts.

Pretty cool, right?

Note… since Visual Studio 2022 17.1 (Preview 2) we can automatically run code cleanup on save.