Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Visual Studio Formatting Options for an entire team

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?

like image 261
Anton Gogolev Avatar asked Jul 27 '12 08:07

Anton Gogolev


People also ask

How do I format all files in Visual Studio?

As long as your code is syntactically correct (i.e. no stray brackets or End Ifs without matching Ifs), Visual Studio will reformat your whole file with one key chord: Hold down the Control key and then press K, followed by D. Boom!

How do I enable formatting in Visual Studio?

In an existing project, open the document that you want to format, right-click inside the document, and select Format Document. In the default configuration for Visual Studio Code, the command can be run using the shortcut Alt+Shift+F.

How do I change the auto format in VS Code?

You can format an entire file with Format Document (Ctrl+Shift+I) or just the current selection with Format Selection (Ctrl+K Ctrl+F) in right-click context menu. You can also configure auto-formatting with the following settings: editor. formatOnSave - to format when you save your file.


2 Answers

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).

like image 102
Frédéric Avatar answered Sep 22 '22 04:09

Frédéric


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

like image 41
Eddy Avatar answered Sep 25 '22 04:09

Eddy