When working with other people and multiple editors/IDEs, it is common to have different editor settings, losing consistency in formatting styles of the code. For example: Using tabs/spaces and different sizes of indentation, making your code harder to read; Using different encoding between files, causing hard to find bugs at runtime (showing invalid characters) and breaking automated tests. In this post I'll show how to maintain a standard for everyone who works in the code, no matter the editor used, and in a next post I'll show how to enforce these (and other) rules on build and in the continuous integration pipeline. Enter the EditorConfig file The EditorConfig file is used by editors and IDEs to define editor preferences for the project. Without it, IDEs and editors will use their general configuration, causing divergences in the files edited on them. Many editors and IDEs support the EditorConfig file , and others . Here are some: by default have plugins to support it Visual Studio (Built-in); JetBrains Rider (Built-in); GitHub (Built-in); VS Code (Plugin); Vim (Plugin); Emacs (Plugin); Sublime (Plugin); It has a default name of and is an INI file where sections are filename filters, for instance: .editorconfig for files; [*.cs] .cs for javascript files inside the folder and subdirectories; [scripts/**.js] scripts for the file only. [{package.json}] package.json More details here The EditorConfig can be put in any directory and be overridden by EditorConfig files in child directories, but for better visibility, they should be in the same directory as the .NET solution file. Adding an EditorConfig to your project To include an EditorConfig to your project, just create a file named in the same directory of your solution file ( ) with the content below. .editorconfig .sln # Top-most EditorConfig file. root = true # Section for C# files # All rules below apply only to .cs files [*.cs] #### Core EditorConfig Options #### # Indentation and spacing indent_style = space indent_size = 4 # New line preferences end_of_line = crlf insert_final_newline = false trim_trailing_whitespace = true # Charset preference charset = utf-8 This file set these rules: : No editorconfig file in parent directories will be read; root : Indentation should use space; indent_style : Indentation should use 4 space characters; indent_size : Lines should end with CR+LF characters; end_of_line : Do not automatically insert empty lines at the end of the files; insert_final_newline : Empty lines should have no space characters; trim_trailing_whitespace : Files should be encoded with UTF-8 format. charset More details . here ⚠️ and change it back to CRLF on checkouts. Then, the settings can be safely set to CRLF. GIT for Windows suggests this configuration by default at installation. Details on how to configure are . GIT can change the line ends to LF on pushes to the repository end_of_line here ⚠️ , according to the Unicode standard. . UTF-8 with BOM is not required nor recommended More here ℹ️ specific to the .NET environment. In a next post, I'll show how to configure these rules. In .NET, the EditorConfig file can also be used to define analyzers rules and severities Appling the new formatting rules to code When we change the formatting rules for an existing project, the changes are not applied automatically. We need to manually trigger an auto-format. Visual Studio First, we have to include the fixer to a Code Cleanup profile. Format document 1 - Navigate to . Analyze > Code Cleanup > Configure Code Cleanup 2 - Include the fixer for the profile you wish to run on save. Format document 3 - On the menu, select . Analyze > Code Cleanup > Run Code Cleanup (Yout Profile) on Solution ⚠️ . You have to use the dotnet-format or another tool for that. Visual Studio's format document doesn't change the file encoding VS Code VS Code doesn't have a feature to format all files at once. You have to use the extension. Format Files 1 - Install the . Format Files extension 2 - Navigate to or press . View > Command Palette Ctrl+Shift+P 3 - Select the or option. Start Format Files: Workspace Start Format Files: From Glob JetBrains Rider Select or press . Code > Reformat Code Ctrl+Alt+Enter Format on save When we create an EditorConfig file, the supported editors will also use the configurations for their auto-format features. Here I show how to enable the auto-format on file save in some editors. Format on save in Visual Studio Visual Studio 2022 doesn't have a format on save feature, but it can run a Code Cleanup on save. This way we can configure a Code Cleanup profile to run a and use it on save. Format document ℹ️ For Visual Studio 2019, there is an that enables the same feature. extension 1 - Configure your Code Cleanup profile to run the fixer, as shown in the previous section of this post. Format document 2 - Navigate to . Tools > Options > Text Editor > Code Cleanup 3 - Check the option and select the Code Cleanup profile to run on save. Run Code Cleanup profile on Save Now Visual Studio will format your files on every save. Format on save in VS Code 1 - Navigate to . File > Preferences > Settings 2 - Navigate to or search for . Text Editor > Formatting editor.formatOnSave 3 - Check the option. Editor: Format On Save Format on save in JetBrains Rider 1 - Navigate to . File > Settings > Tools > Actions on Save 2 - Check the option. Reformat and Cleanup Code 3 - Select the profile. Reformat Code References and Links https://editorconfig.org/ https://devblogs.microsoft.com/visualstudio/bringing-code-cleanup-on-save-to-visual-studio-2022-17-1-preview-2/ https://learn.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2022 https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-format Liked this post? I post extra content in my personal blog. to see. Click here Also published . here