Overview

EditorConfig is a cross-platform file format for defining code styles.

File Format

  • EditorConfig files consist of preambles (the lines that precede the first section), section names (between brackets), and sections (which run from the beginning of the section header to the beginning of the next section header).
  • Comments start with ; or #
    • No inline comments

Section Headers are enclosed in [ ]

  • No non-whitespace characters outside brackets
  • May contain characters and whitespace between brackets
  • Path separators are always /

Sections

Key-Value Pairs are of format key=value

Globs

CharacterMatches
*any string except /
``any string
?any single character except /
[seq]any single character in seq
[!seq]any single character not in seq
{s1, s2, s3}any of these strings
{1..5}any digit 1 through 5

Properties

Widely-supported Properties

PropertyDefinitionPossible values
indent_styleThe indentation styletab, space
indent_sizeThe size of indentationinteger, tab
tab_widthWidth of a single tabstopinteger
end_of_lineLine ending file formatlf, crlf, cr
charsetFile character encodinglatin1, utf-8, utf-16be, utf16-le, utf-8-bom
trim_trailing_whitespaceWhether whitespace is removed from EOLtrue, false
insert_final_newlineWhether a file should end in a newlinetrue, false
rootIdentify the root editorconfig (must be set in preamble)true, false

Limited Properties

PropertyDefinitionPossible values
max_line_lengthForce hard line wrapping after n charactersinteger, off

For any property, a value of unset removes the effect of that property, even if it was set before.

EditorConfig in Visual Studio

  • EditorConfig settings take precedence over global Visual Studio text editor settings.
  • The formatting of existing code is not changed unless Code Cleanup is run (for all settings) or Format Document is run (for whitespace settings only).

Creating

Project > Add New Item > editorconfig

  • editorconfig File (default) comes pre-populated with two items.
  • editorconfig File (.NET) comes pre-populated with .NET code style rules.

Applying

EditorConfig files are searched for in the current directory and all parent directories until the root file is found or the root of the filesystem is reached.

To quickly find editorconfig files, use dir .editorconfig /s

Disable EditorConfig

Tools > Options > Text Editor > uncheck Follow project coding conventions

Code Style & Code Quality Rules

EditorConfig can also express code style and quality rules.

This format is only understood by Visual Studio while coding, not the compiler:
dotnet_style_readonly_field = true:suggestion

This format is understood by the compiler at build time:
dotnet_diagnostic.ruleID.severity = severity-level

Severity Levels

Severity (Solution Explorer)Severity (EditorConfig file)Build-time behavior Editor behavior
ErrorerrorViolations appear asErrorsin the Error List and in command-line build output, and cause builds to fail.Offending code is underlined with a red squiggle and marked by a small red box in the scroll bar.
WarningwarningViolations appear asWarningsin the Error List and in command-line build output, but don’t cause builds to fail.Offending code is underlined with a green squiggle and marked by a small green box in the scroll bar.
InfosuggestionViolations appear asMessagesin the Error List, and not at all in command-line build output.Offending code is underlined with a gray squiggle and marked by a small gray box in the scroll bar.
HiddensilentNon-visible to user. Non-visible to user. The diagnostic is reported to the IDE diagnostic engine, however.
NonenoneSuppressed completely. Suppressed completely.
DefaultdefaultCorresponds to the default severity of the rule. To determine what the default value for a rule is, look in the Properties window.Corresponds to the default severity of the rule.

Sample editorconfig Files

Suppressing EditorConfig Violations

#pragma warning disable CAXXXX

AnalyzerConfig

Configuration files for code analysis rules - .NET | Microsoft Learn
Like EditorConfig, but project-level configuration options.
Use when you have project files outside the project folder.

See Also