Search code examples
visual-studiocode-formattingeditorconfig

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


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.


Solution

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