How to import an EditorConfig file into Visual Studio for all solutions?
Asked Answered
L

2

31

How do I import code conventions (code formatting settings) from an EditorConfig file into Visual Studio?

I would like to have one .editorconfig file for all solutions and not one per project.

Lover answered 30/10, 2019 at 13:14 Comment(0)
N
32

EditorConfig is supported in Visual Studio 2017 and newer.

Creating an EditorConfig file

If you like you can create an empty text file and just add the settings you want. However, Visual Studio can create a .editorconfig file with sensible defaults for .NET for you. From MSDN:

  • From the menu bar, choose Project > Add New Item; or press Ctrl+Shift+A

  • Select the editorconfig File (.NET) template to add an EditorConfig file prepopulated with default .NET code style, formatting, and naming conventions

enter image description here

Visual Studio 2019 - Creating an EditorConfig file from current settings

In Visual Studio 2019, you can instead create an EditorConfig file from your current settings. Just click the following button in the Options dialog under Text Editor > C# > Code Style > General:

enter image description here

Sharing an EditorConfig across solutions

To share settings across solutions and projects, you need to create a .editorconfig file in a top-level directory/solutions folder. For example, I have mine in trunk and I commit it to source control so that my colleagues share the same settings.

The settings in this file apply to all projects in trunk and subfolders, unless overridden by another .editorconfig file further down the folder tree e.g. you might you have a project specific EditorConfig file in a subfolder which has different settings. You don't need to "import" the file - it should be detected automatically.

See File hierarchy and precedence for more details.

If you're using Visual Studio 2019, I suggest you use the Generate .editorconfig file from settings button detailed above, and save the .editorconfig file to your solutions folder. Otherwise, you can create a throwaway project, create the config file, and then cut and paste that file to the folder where it needs to live.

Neilla answered 30/10, 2019 at 23:30 Comment(5)
Is it possible for others to update this .editorconfig using their Visual Studio's Tools, Options, Code Style editor?Digiacomo
it's a shame but it seems that it is not possible to save the modifications made from the option menu on the editorconfig filesDistefano
Resharper has the option to generate it too..., basically it will 'export' all Reshaper settings into .editorconfigUnfolded
Is it possible to use .editorconfig on solutionitems which aren't in a project?Acetamide
I've understood how that works even before reading this answer, but I've left one question: To me, it seems that we need different .editorconfig files for C++, C# and Javascript. Is it possible to have multiple .editorconfig files at the solution level, e.g. because the solution incorporates one C# and one C++ project? (I believe it is not possible, because it seems that the .editorconfig file must have exactly that name).Paleo
S
-3

Put the .editorconfig in a base folder shared by all of your repositories.

Visual Studio and VS Code will pick it up from there for all your workspaces.

If you cloned your code using the Visual Studio default location, then the common base folder will be %USERPROFILE%\source\repos\

Sarco answered 29/6, 2022 at 20:13 Comment(10)
"Visual Studio and VS Code will pick it up from there for all your solutions because typically all of our source code goes in %USERPROFILE%\source\repos." Sorry, but I don't think that's accurate at all.Lashkar
@Lashkar That's the default VS behavior. How accurate do you want me to be?Sarco
See Microsoft if you don't believe me: learn.microsoft.com/en-us/visualstudio/ide/…Sarco
I think you misunderstood me. I agree that if your repo is in that folder your solution will work, but it's not a universal solution. I don't think "typically" all our source code goes there. Maybe your personal projects, but working at companies you "typically" sync an existing codebase. The link you posted is useful but doesn't mention anything regarding that filepath being the most common. I think a better answer would be explaining that it should be put in a root directory of sorts, and then "for example, if your code is in %USERPROFILE%\source\repos, then put it in your user directory"Lashkar
@Lashkar Visual Studio uses that directory to clone repos to. You have to explicitly decide to not use that default location when cloning a repo using Visual Studio. My answer is 100% accurate and calls out the fact that it's relying on the default repo location. If you're using a non-standard location for your synced source code, I'd expect you to be able to figure out the appropriate parent directory to put the .editorconfig. You're right that I can add that guidance to the end of my answer, and it would help people connect the dots.Sarco
I have never had code in that location myself, and I would not expect that any enterprise developers have the code there. This may be a default location for Visual Studio, but VS is but one of dozens of tools used for cloning Git repositories. The second paragraph is incorrect, hence the suggested solution is typically incorrect.Gromme
@NikolaMalešević This question and answer are for Visual Studio. So why would you assume that most developers looking at this didn't use Visual Studio to clone their git repository?Sarco
Because most developers looking at this are here because of .editorconfig issue. They come from different backgrounds, and although they use Visual Studio, they most probably do not use the location you mentioned for their code.Gromme
You can take a look at the most recent Stack Overflow developer survey: far more people use a command line tool than a code editor. survey.stackoverflow.co/2022/#technology-version-controlGromme
@NikolaMalešević That survey shows that over half of all developers use the Code Editor provided git interface. I'm guessing that depends on whether their given language's code editor has good git integration. VS has good git integration and can be opened directly from GitHub. So I'd imagine if that data were filtered by VS users, it would be close to 100% for the Code Editor option.Sarco

© 2022 - 2024 — McMap. All rights reserved.