Saving and Sharing Visual Studio Editor and Code Format Settings with team members

Have you ever wanted to check into source control with a project or solution the tab spacing, indent size and line endings for that solution and have them automatically applied when Visual Studio opens that solution? And to also have these settings automatically applied when anyone opens the solution or project?

Well, this can be quite simply achieved using a Visual Studio extension called Editor Config. (Visual Studio Extension Github Page)

First, install the ‘EditorConfig’ Visual Studio Extension (using Tools > Extensions and Updates > Visual Studio Gallery)

The way that EditorConfig works is that it looks for an ‘.editorconfig’ file which contains the settings for the project or solution (depending on where the config file is located within the folder hierarchy and if the configuration file is marked as ‘root’, or the ‘solution’ configuration file).

In my case I’d like to define the tab size, indent size and line endings at the solution level so this is how you can achieve that:

1. Create an ‘.editorconfig’ file in the same directory as your solution file.

You can do this by simply navigating in Windows Explorer to where your solution file resides, creating a new text document and renaming that to the desired ‘.editorconfig’.

NOTE: Windows Explorer will try to stop you from creating a file without a file name such as ‘.editorconfig’ by popping up this ‘You must type a file name’ error message:

You can easily bypass this by appending an extra ‘.’ at the end of your filename, which will make Windows Explorer create the file, without the extra ‘.’

So in this case, if you rename the text file to ‘.editorconfig.’, Windows Explorer will not pop up the error message and create the ‘.editorconfig’ file as we want:

2. Now we are ready to define the Editor Config settings for our solution by editing our newly created ‘.editorconfig’ file in a text editor. For example, the configuration file that I use is as follows:

# top-most / Solution EditorConfig file
root = true

# Windows-style newlines
end_of_line = crlf

# 3 space indentation
[*] # apply to every file type, you can filter to just csharp files by using [*.cs]
indent_style = space
indent_size = 3

After saving this file, whenever your teammates or you yourself open this solution in Visual Studio, the ‘EditorConfig’ Extension will automatically read in the above settings. (Provided that you have installed the Editor Config extension first)

You can test this by running a ‘Format Document’ on a class file in your solution (via the Edit > Advanced > ‘Format Document’ menu item).