|
| 1 | +--- |
| 2 | +title: Code style options and code cleanup |
| 3 | +ms.date: 04/25/2019 |
| 4 | +ms.topic: conceptual |
| 5 | +author: gewarren |
| 6 | +ms.author: gewarren |
| 7 | +manager: jillfra |
| 8 | +f1_keywords: |
| 9 | + - "VS.ToolsOptionsPages.Text_Editor.CSharp.Code_Style.General" |
| 10 | + - "VS.ToolsOptionsPages.Text_Editor.Basic.Code_Style.General" |
| 11 | +ms.workload: |
| 12 | + - "multiple" |
| 13 | +--- |
| 14 | +# Code style preferences |
| 15 | + |
| 16 | +You can define code style settings per-project by using an [EditorConfig file](#code-styles-in-editorconfig-files), or for all code you edit in Visual Studio on the text editor [**Options** page](#code-styles-in-the-options-dialog-box). For C# code, you can also configure Visual Studio to apply these code style preferences using the **Code Cleanup** (Visual Studio 2019) and **Format Document** (Visual Studio 2017) commands. |
| 17 | + |
| 18 | +> [!NOTE] |
| 19 | +> This topic applies to Visual Studio on Windows. For Visual Studio for Mac, see [Editor behavior in Visual Studio for Mac](/visualstudio/mac/editor-behavior). |
| 20 | +
|
| 21 | +## Code styles in EditorConfig files |
| 22 | + |
| 23 | +[Code style settings](../ide/editorconfig-code-style-settings-reference.md) for .NET can be specified by adding an [EditorConfig](create-portable-custom-editor-options.md) file to your project. EditorConfig files are associated with a codebase rather than a Visual Studio personalization account. Settings in an EditorConfig file take precedence over code styles that are specified in the **Options** dialog box. Use an EditorConfig file when you want to enforce coding styles for all contributors to your repo or project. |
| 24 | + |
| 25 | +::: moniker range=">=vs-2019" |
| 26 | + |
| 27 | +You can manually populate your EditorConfig file, or you can automatically generate the file based on code style settings you've set in the Visual Studio **Options** dialog box for the C# or Visual Basic text editor. This options page is available at **Tools** > **Options** > **Text Editor** > [**C#** or **Basic**] > **Code Style** > **General**. |
| 28 | +Click **Generate .editorconfig file from settings** to automatically generate a coding style *.editorconfig* file based on the settings on this **Options** page. |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +::: moniker-end |
| 33 | + |
| 34 | +## Code styles in the Options dialog box |
| 35 | + |
| 36 | +Code style preferences can be set for all of your C# and Visual Basic projects by opening the **Options** dialog box from the **Tools** menu. In the **Options** dialog box, select **Text Editor** > [**C#** or **Basic**] > **Code Style** > **General**. |
| 37 | + |
| 38 | +Each item in the list shows a preview of the preference when selected: |
| 39 | + |
| 40 | +::: moniker range="vs-2017" |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +::: moniker-end |
| 45 | + |
| 46 | +::: moniker range=">=vs-2019" |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +::: moniker-end |
| 51 | + |
| 52 | +Options set in this window are applicable to your Visual Studio personalization account and aren't associated with a particular project or codebase. In addition, they aren't enforced at build time, including in continuous integration (CI) builds. If you want to associate code style preferences with your project and have the styles enforced during build, specify the preferences in an [.editorconfig file](#code-styles-in-editorconfig-files) that's associated with the project. |
| 53 | + |
| 54 | +### Preference and severity |
| 55 | + |
| 56 | +For each code style setting on this page, you can set the **Preference** and **Severity** values using the drop-downs on each line. Severity can be set to **None**, **Suggestion**, **Warning**, or **Error**. If you want to enable [Quick Actions](../ide/quick-actions.md) for a code style, ensure that the **Severity** setting is set to something other than **None**. The **Quick Actions** light bulb , error light bulb , or screwdriver  icon appears when a non-preferred style is used, and you can choose an option on the **Quick Actions** list to automatically rewrite code to the preferred style. |
| 57 | + |
| 58 | +## Apply code styles |
| 59 | + |
| 60 | +::: moniker range="vs-2017" |
| 61 | + |
| 62 | +You can configure the **Format Document** command (**Edit** > **Advanced** > **Format Document**) to apply your code style settings (from an EditorConfig file or **Code Style** options) along with the regular formatting that it does (such as indentation). If an *.editorconfig* file exists for the project, those settings take precedence. |
| 63 | + |
| 64 | +> [!NOTE] |
| 65 | +> Applying code styles by using the **Format Document** command is only available for C# code files. This is an experimental feature. |
| 66 | +
|
| 67 | +Configure which settings you want **Format Document** to apply on the [Formatting options page](reference/options-text-editor-csharp-formatting.md#format-document-settings). |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +> [!TIP] |
| 72 | +> Rules configured with a severity of **None** don't participate in code cleanup but can be individually applied via the **Quick Actions and Refactorings** menu. |
| 73 | +
|
| 74 | +The first time you trigger the **Format Document** command, a yellow info bar prompts you to configure your code cleanup settings. |
| 75 | + |
| 76 | +::: moniker-end |
| 77 | + |
| 78 | +::: moniker range=">=vs-2019" |
| 79 | + |
| 80 | +For C# code files, Visual Studio 2019 has a **Code Cleanup** button at the bottom of the editor (keyboard: **Ctrl**+**K**, **Ctrl**+**E**) to apply code styles from an EditorConfig file or from the **Code Style** options page. If an *.editorconfig* file exists for the project, those are the settings that take precedence. |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +> [!TIP] |
| 85 | +> Rules configured with a severity of **None** don't participate in code cleanup but can be individually applied via the **Quick Actions and Refactorings** menu. |
| 86 | +
|
| 87 | +First, configure which code styles you want to apply (in one of two profiles) in the **Configure Code Cleanup** dialog box. To open this dialog box, click the expander arrow next to the code cleanup broom icon and then choose **Configure Code Cleanup**. Or, press **Ctrl**+**K**, **Ctrl**+**Q**. |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +If you want your code style settings to be applied every time you save a file, you may like the [Code Cleanup on Save](https://marketplace.visualstudio.com/items?itemName=MadsKristensen.CodeCleanupOnSave) extension. |
| 92 | + |
| 93 | +::: moniker-end |
| 94 | + |
| 95 | +## See also |
| 96 | + |
| 97 | +- [Quick Actions](../ide/quick-actions.md) |
| 98 | +- [.NET coding convention settings for EditorConfig](../ide/editorconfig-code-style-settings-reference.md) |
| 99 | +- [Editor behavior (Visual Studio for Mac)](/visualstudio/mac/editor-behavior) |
0 commit comments