Creating and editing a custom EditorConfig file in Visual Studio for Mac
Important
Visual Studio for Mac was retired on August 31, 2024 in accordance with Microsoft’s Modern Lifecycle Policy. While you can continue to work with Visual Studio for Mac, there are several other options for developers on Mac such as the preview version of the new C# Dev Kit extension for VS Code.
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:
indent_style
indent_size
tab_width
end_of_line
charset
trim_trailing_whitespace
insert_final_newline
root
EditorConfig also supports Coding conventions in C#.
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
.editorconfig
file 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.editorconfig
file to an existing source file, open the file and choose Edit > Format > Format Document from the menu bar::
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
.
(such as.editorconfig
) are hidden files in macOS, so press Command + Shift + . to make the.editorconfig
file visible.Select the
.editorconfig
file and click Open:When you're presented with the following dialog, select the Copy the file to the directory option and select OK:
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:
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
Setting root
to 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:
Before:
After:
For more information on available EditorConfig settings, see the .NET coding convention settings for EditorConfig article and the Supported Properties section on the official documentation.
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 root=true
.
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 .editorconfig
file:
# top-most EditorConfig file
root = true