You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/ide/cpp-editorconfig-properties.md
+11-5Lines changed: 11 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
title: "C++ EditorConfig formatting conventions"
3
3
titleSuffix: ""
4
4
description: "Learn about how to use EditorConfig to format C++ code in Visual Studio."
5
-
ms.date: 9/14/2020
5
+
ms.date: 08/22/2024
6
6
author: jureid
7
7
ms.author: ghogen
8
8
manager: mijacobs
@@ -11,17 +11,23 @@ dev_langs:
11
11
- CPP
12
12
13
13
ms.topic: reference
14
-
monikerRange: vs-2019
15
14
---
16
15
# C++ EditorConfig formatting conventions
17
16
18
-
The Visual Studio C++ formatter has a rich set of configurable settings that can be applied globally. To set C++ formatting settings for a specific workspace, use [clangformat](https://clang.llvm.org/docs/ClangFormat.html) or [EditorConfig](https://editorconfig.org/). Both Visual Studio and Visual Studio Code have built-in EditorConfig support for each of the global Visual Studio C++ formatting settings, with the EditorConfig settings taking precedence. This means you can add EditorConfig files to your workspace to configure C++ formatting on a more granular level and enforce consistent code style for everyone contributing to the project.
17
+
You can add EditorConfig files to your project to configure C++ formatting to enforce a consistent code style for everyone contributing to the project. Both Visual Studio and Visual Studio Code have built-in [EditorConfig](https://editorconfig.org/) support for each of the global Visual Studio C++ formatting settings. The EditorConfig settings are stored in an `.editorconfig` file. When that file is present in your project, it takes precedence over the Visual Studio formatting settings.
18
+
19
+
This document lists all the EditorConfig C++ formatting settings supported by Visual Studio and Visual Studio Code.
20
+
21
+
For more information about:
22
+
23
+
- Using an EditorConfig file in Visual Studio, see [Define consistent coding styles with EditorConfig](create-portable-custom-editor-options.md).
24
+
- Manually create an EditorConfig file based on clang formatting settings, see [clangformat](https://clang.llvm.org/docs/ClangFormat.html). Rename that file `.editorconfig` and save it in your project.
19
25
20
26
## C++ formatting conventions
21
27
22
-
C++ formatting EditorConfig settings are prefixed with `cpp_`. Here's an example of what your EditorConfig file might look like:
28
+
C++ formatting EditorConfig settings are prefixed with `cpp_`. Here's an example of what an `.editorconfig` file might look like:
Copy file name to clipboardExpand all lines: docs/ide/create-portable-custom-editor-options.md
+17-13Lines changed: 17 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -15,9 +15,13 @@ ms.date: 04/01/2024
15
15
---
16
16
# Define consistent coding styles with EditorConfig
17
17
18
-
To enforce consistent coding styles for everyone that works in a codebase, you can add an EditorConfig file to your solution or project. EditConfig file settings adhere to a file format specification maintained by [EditorConfig.org](https://editorconfig.org). Many code editors and applications support EditorConfig files, including Visual Studio. It's a portable component that travels with your code, and can enforce coding styles even outside of Visual Studio.
18
+
To enforce consistent coding styles for everyone that works in a codebase, you can add an `.editorConfig` file to your solution or project. EditorConfig file settings adhere to a file format specification maintained by [EditorConfig.org](https://editorconfig.org). Many code editors and applications support EditorConfig files, including Visual Studio. Because the settings are in a file, they accompany your code, and can be used even outside of Visual Studio.
19
19
20
-
In Visual Studio, EditorConfig file settings take precedence over the global text editor settings (accessed by selecting **Text Editor** in the **Tools** > **Options** dialog box). This means that you can tailor each codebase to use text editor settings that are specific to that project. When you use EditorConfig files, you can still continue to set your own personal editor preferences in the Visual Studio text editor. These text editor settings apply whenever you're working in a codebase without an EditorConfig file, or when the EditorConfig file doesn't override a particular setting. An example of such a preference is whether to use tabs or spaces for the code indent style.
20
+
In Visual Studio, EditorConfig file settings take precedence over the various global text editor settings available under **Tools** > **Options** > **Text Editor** > **C/C++** > **Code Style**. You can use an EditorConfig file to tailor each codebase to use text editor settings that are specific to that project.
21
+
22
+
You can manually populate an EditorConfig file, or automatically generate it based on the code style settings you've chosen in Visual Studio. To create a new `.editorconfig` file based on your current Visual Studio settings, from the Visual Studio main menu choose **Tools** > **Options** > **Text Editor** > **C/C++** > **General**. Then choose **Generate .editorconfig file from settings**. Choose the folder where your project is and then choose **Select Folder** to save the `.editorconfig` file.
23
+
24
+
When you use an EditorConfig file, you can still continue to set your own personal editor preferences in the Visual Studio text editor. These text editor settings apply whenever you're working in a codebase without an EditorConfig file, or when the EditorConfig file doesn't override a particular setting. An example of such a preference is whether to use tabs or spaces for the code indent style.
21
25
22
26
When you add an EditorConfig file to your project in Visual Studio, only new lines of code are formatted based on the EditorConfig settings. The formatting of existing code isn't changed unless you run one of the following commands:
23
27
@@ -38,7 +42,7 @@ EditorConfig file settings let you maintain consistent coding styles and convent
38
42
39
43
EditorConfig files are useful when coding conventions you use on your personal projects differ from those conventions used on your team's projects. For example, you might prefer that an indent adds a tab character in your code. However, your team might prefer that an indent adds four space instead. EditorConfig files resolve this problem by letting you have a configuration for each scenario.
40
44
41
-
Because an *.editorconfig* file in the codebase contains the EditorConfig settings, they travel along with that codebase. As long as you open the code file in an EditorConfig-compliant editor, the text editor settings are activated.
45
+
Because an `.editorconfig` file in the codebase contains the EditorConfig settings, they travel along with that codebase. As long as you open the code file in an EditorConfig-compliant editor, the text editor settings are activated.
42
46
43
47
::: moniker range="<=vs-2019"
44
48
> [!NOTE]
@@ -72,7 +76,7 @@ If you remove an EditorConfig file from your project or codebase, you must close
72
76
73
77
To add an EditorConfig file to your project or solution, follow these steps:
74
78
75
-
1. Open a project or solution in Visual Studio. Select either the solution or project node, depending on whether your EditorConfig settings should apply to all projects in the solution or just one. You can also select a folder in your project or solution to add the *.editorconfig* file to.
79
+
1. Open a project or solution in Visual Studio. Select either the solution or project node, depending on whether your EditorConfig settings should apply to all projects in the solution or just one. You can also select a folder in your project or solution to add the `.editorconfig` file to.
76
80
77
81
1. From the menu, choose **Project** > **Add New Item**, or press **Ctrl**+**Shift**+**A**.
78
82
@@ -86,7 +90,7 @@ To add an EditorConfig file to your project or solution, follow these steps:
86
90
87
91
1. Select the **editorconfig File (empty)** template to add an EditorConfig file prefilled with default EditorConfig options for whitespace, code style, and naming conventions. Or, select the **editorconfig File (.NET)** template to add an EditorConfig file prefilled with default [.NET whitespace, code style, and naming conventions](/dotnet/fundamentals/code-analysis/code-style-rule-options).
88
92
89
-
A new *.editorconfig* file appears in Solution Explorer, and opens in the editor as a new tab.
93
+
A new `.editorconfig` file appears in Solution Explorer, and opens in the editor as a new tab.
90
94
91
95
::: moniker range=">= vs-2022"
92
96
:::image type="content" source="media/vs-2022/editorconfig-dotnet-new.png" alt-text="Screenshot that shows the .editorconfig file in Solution Explorer and editor.":::
@@ -98,23 +102,23 @@ To add an EditorConfig file to your project or solution, follow these steps:
98
102
99
103
There are a couple of other ways you can add an EditorConfig file to your project:
100
104
101
-
- Use [IntelliCode for Visual Studio](/visualstudio/ide/intellicode-visual-studio) to generate an *.editorconfig* file in your solution. Right-click in Solution Explorer and choosing **Add** > **New EditorConfig (IntelliCode)** from the context menu. IntelliCode for Visual Studio infers your code styles from existing code, and then creates an EditorConfig file with your code-style preferences predefined.
105
+
- Use [IntelliCode for Visual Studio](/visualstudio/ide/intellicode-visual-studio) to generate an `.editorconfig` file in your solution. Right-click in Solution Explorer and choosing **Add** > **New EditorConfig (IntelliCode)** from the context menu. IntelliCode for Visual Studio infers your code styles from existing code, and then creates an EditorConfig file with your code-style preferences predefined.
102
106
103
107
-[Automatically generate an EditorConfig file based on your code-style settings](code-styles-and-code-cleanup.md#code-styles-in-editorconfig-files).
104
108
105
109
## File hierarchy and precedence
106
110
107
-
When you add an *.editorconfig* file to a folder in your file hierarchy, its settings apply to all applicable files at that level and lower. You can also override EditorConfig settings for a particular project, codebase, or part of a codebase, such that it uses different conventions than other parts of the codebase. Doing so can be useful when you incorporate code from somewhere else, and don’t want to change its conventions.
111
+
When you add an `.editorconfig` file to a folder in your file hierarchy, its settings apply to all applicable files at that level and lower. You can also override EditorConfig settings for a particular project, codebase, or part of a codebase, such that it uses different conventions than other parts of the codebase. Doing so can be useful when you incorporate code from somewhere else, and don’t want to change its conventions.
108
112
109
113
Follow these guidelines:
110
114
111
-
- To override some or all of the EditorConfig settings, add an *.editorconfig* file at the level of the file hierarchy you want those overridden settings to apply. The new EditorConfig file settings apply to files at the same level and files in any subdirectories.
115
+
- To override some or all of the EditorConfig settings, add an `.editorconfig` file at the level of the file hierarchy you want those overridden settings to apply. The new EditorConfig file settings apply to files at the same level and files in any subdirectories.
112
116
113
117
:::image type="content" source="../ide/media/vside_editorconfig_hierarchy.png" alt-text="Screenshot that shows the EditorConfig hierarchy.":::
114
118
115
-
- If you want to override some, but not all of the settings, specify just those settings in the *.editorconfig* file. Only those properties that you explicitly list in the lower-level *.editorconfig* file are overridden. Other settings from any higher-level *.editorconfig* files continue to apply.
119
+
- If you want to override some, but not all of the settings, specify just those settings in the `.editorconfig` file. Only those properties that you explicitly list in the lower-level `.editorconfig` file are overridden. Other settings from any higher-level `.editorconfig` files continue to apply.
116
120
117
-
- 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 lower-level *.editorconfig* file.
121
+
- 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 lower-level `.editorconfig` file.
118
122
119
123
```ini
120
124
# top-most EditorConfig file for this level
@@ -167,7 +171,7 @@ The following example shows the indent state of a C# code snippet before and aft
167
171
::: moniker-end
168
172
169
173
::: moniker range="vs-2019"
170
-
Add a new file named *.editorconfig* to the project, with the following contents. The `[*.cs]` indicator means that this change applies only to C# code files in the project.
174
+
Add a new file named `.editorconfig` to the project, with the following contents. The `[*.cs]` indicator means that this change applies only to C# code files in the project.
171
175
172
176
```ini
173
177
# Top-most EditorConfig file
@@ -209,9 +213,9 @@ To troubleshoot EditorConfig issues, follow these steps:
209
213
dir .editorconfig /s
210
214
```
211
215
212
-
1. To control the scope of your EditorConfig conventions, set the `root=true` property in the *.editorconfig* file at the root of your repo or in the directory that your project resides.
216
+
1. To control the scope of your EditorConfig conventions, set the `root=true` property in the `.editorconfig` file at the root of your repo or in the directory that your project resides.
213
217
214
-
Visual Studio looks for a file named *.editorconfig* in the directory of the opened file and in every parent directory. The search ends when it reaches the root filepath, or if an *.editorconfig* file with `root=true` is found.
218
+
Visual Studio looks for a file named `.editorconfig` in the directory of the opened file and in every parent directory. The search ends when it reaches the root filepath, or if an `.editorconfig` file with `root=true` is found.
0 commit comments