Set Visual Studio Formatting Options for an entire team
Asked Answered
A

2

28

Different devs on our team have wildly different checkboxes here:

Visual Studio Formatting Options

and as a consequence Visual Studio keeps reformatting code and this really pollutes our commits.

What I want is to have a single whatever (.reg file or something) to run on each devs' computer so that these settings will be consistent.

How can I do this?

Axletree answered 27/7, 2012 at 8:49 Comment(1)
Related: #40212Lasky
C
21

Starting from VS2017, .editorconfig files are taken into account and allow to override local preferences. Put it at the root of your projects solution (or even higher), under source control in order to distribute it with your sources to each developer.

You can set .Net coding conventions through VS (since v15.3) specific properties, documented here.

Example file:

root = true

[*]
end_of_line = CRLF
insert_final_newline = true

[*.cs]
indent_style = tab
dotnet_sort_system_directives_first = true
csharp_space_after_cast = true

[*.xsd]
indent_style = tab

[*.json]
indent_style = space
indent_size = 2

[*.xml]
indent_style = space
indent_size = 2

[*.cshtml]
indent_style = space
indent_size = 4

For VS2017, the IntelliCode experimental extension has a feature for generating editor config files, see this blog post for more information.

Starting from VS2019, VS can create an editorconfig file from "New File" templates, from your code base (right-click a project > Add > New EditorConfig) or from your current VS configuration (Tools > Options > Text Editor > [C# or Basic] > Code Style > General).

Cheyenne answered 31/8, 2017 at 18:25 Comment(3)
Is there a way of automatically exporting .editorconfig according to my current settings? Doing it by hand and looking up every single property would take hours.Confront
Good question blade. I added a suggestion here that you can vote for. I too think that it would be great to be able to export the clang or editor.config file from current options initially instead of trying to manually recreate an entire config file. visualstudio.uservoice.com/forums/121579-visual-studio-ide/…Grandma
It seems to be available since July, see the last sentence I have added to my answer.Unset
A
13

You can export the desired settings from one of the visual studio instances from the tools menu using the option "Import and Export settings". This will save the settings to a .vssettings file (which is actually a xml file) holding stuff like

<PropertyValue name="TabSize">4</PropertyValue>

You can then either import these settings on the other machines through the user interface (same menu option) or you can load them from the commandline using

devenv.exe /Resetsettings <your settingsfile>

This commandline settings is documented here

Anniceannie answered 27/7, 2012 at 9:24 Comment(3)
In Visual Studio 2013, this does not export the formatting settings Anton showed in his screenshot.Slusher
IN VS2102 exporting "All Settings/Options/Text Editor/C# Editor" does export these settings e.g. <PropertyValue name="NewLines_Braces_Method">1</PropertyValue>Corvine
Same for VS2013 You can exactly specify what is and what isn't exported.Anniceannie

© 2022 - 2024 — McMap. All rights reserved.