Creating and editing a custom EditorConfig file
Applies to: Visual Studio for Mac Visual Studio
In Visual Studio for Mac, you can add an EditorConfig file to your project or solution to enforce consistent coding styles for everyone that works in the codebase. The settings declared in the EditorConfig file take precedence over global Visual Studio for Mac text editor settings. Using an EditorConfig file within your project or codebase allows you to set your coding style, preferences, and warnings for your project. Because the file is part of your codebase, it makes it easier for all users to adhere to the coding practices of a project, regardless of the IDE or code editor that they use.
EditorConfig files are supported on many IDEs and code editors, including Visual Studio.
The editor in Visual Studio for Mac supports the core set of EditorConfig properties:
EditorConfig also supports Coding conventions in C#.
Add an EditorConfig file to a project
Adding a new EditorConfig file
Open your project in Visual Studio for Mac. Select either the solution or project node that you wish to add the EditorConfig file to. Adding the file to the solution directory applies the .editorconfig settings to all projects in the solution.
Right-click on the node and select Add > New File to open the New File dialog:
Choose Misc > Empty Text File and give it the Name
.editorconfig. Press New to create the file and open it in the editor:
Adding the item at the solution level automatically creates and nests it in a Solution Items folder:
Edit the file. For example:
# This file is the top-most EditorConfig file root = true # All Files [*] indent_style = space indent_size = 8 insert_final_newline = false trim_trailing_whitespace = false [*.cs] csharp_new_line_before_open_brace = none
The settings from the
.editorconfigfile will apply to any new code that you write, but existing code may need to be reformatted to be consistent with the new settings. To apply the settings from the
.editorconfigfile to an existing source file, open the file and choose Edit > Format > Format Document from the menu bar::
Adding an existing EditorConfig file
If you're working with a project or solution that already contains an
.editorconfig file, there is nothing that you need to do to apply the settings. Any new lines of code are formatted according to the EditorConfig settings.
You may want to reuse an existing
.editorconfig file in your project. To add an existing file, do the following:
Right-click on the folder you wish to add it to and select Add > Add Files.
Browse to the directory of the required file.
Files beginning with
.editorconfig) are hidden files in macOS, so press Command + Shift + . to make the
.editorconfigfile and click Open:
When you're presented with the following dialog, select the Copy the file to the directory option and select OK:
Reflecting .editorconfig settings
Once you add an EditorConfig file to your codebase, any new code added is automatically formatted according to the specified settings. Existing code doesn't automatically reflect the settings unless you format the codebase.
To reflect the settings from the
.editorconfig file, select the solution node and choose Edit > Format > Format Document from the menu bar:
Editing an EditorConfig file
EditorConfig files use a straightforward file layout to specify settings, which is explained below using a previous example:
# This file is the top-most EditorConfig file root = true # All Files [*] indent_style = space indent_size = 4 insert_final_newline = false trim_trailing_whitespace = false [*.cs] csharp_new_line_before_open_brace = none
true flags this file as the top-most file of the codebase and any higher
.editorconfig files in the project are ignored, as explained in the Override EditorConfig Settings section.
Each section is denoted by square ([ ]) braces and specifies information on the types of files the following properties should pertain to.
In the example above, some settings are applied to all files in the project and others are added only to C# files. The screenshots below show before and after the
.editorconfig settings have been applied:
Override EditorConfig Settings
It's possible to have more than one
.editorconfig file in each solution. Visual Studio for Mac reads
.editorconfig files from top to bottom in the solution, adding and overriding settings as it goes.This means that the settings in the
.editorconfig closest to the file you're editing will take precedence. Settings are taken from the
.editorconfig file the same folder (if it exists), then the
.editorconfig in parent folder (if that exists), etc. until it finds
If you want to ensure that no settings from any higher-level
.editorconfig files are applied to this part of the codebase, add the
root=true property to the top of the lower-level
# top-most EditorConfig file root = true