I use TABS
as the whitespace character, CRLF
as the line break thing, and I position my curly braces like this:
private bool Example() {
return true;
}
So, not like this:
private bool Example()
{
return false;
}
I have my VS 2022 C# text editor settings set to be the same way. Still, somehow whenever I use autocompleted code, for example ctor + [tab]
, or when Visual Studio adjusts namespaces in a file, it always uses the exact opposite of what my setting are. It is really annoying and I don’t want to have to fix it manually every time…
I’ve scoured the settings in Visual Studio, but I didn’t find anything else that could affect these things. How could I change the autoformatting to use the same settings as the C# text editor? Thanks.
2
Answers
I have made some additions based on the information provided by user246821. Here is a tested approach which can come into force.
You may try creating a .editorConfig file as described in Code-style rule options and Code style preferences.
In Visual Studio, change your settings as desired.
For example, if one would like to have the following code style:
Set desired settings:
In the Visual Studio menu:
Generate .editorconfig:
In the Visual Studio menu:
Note: If one would like the .editorconfig file to apply to all of one’s projects, ensure that you save it to the parent directory that contains all of your projects. For example, if one stores all project/solutions in
%UserProfile%DocumentsProjects
, then save the .editorconfig in%UserProfile%DocumentsProjects
.For more information see EditorConfig File hierarchy and precedence
According to Define consistent coding styles with EditorConfig:
Alternatively, one may follow the steps described in Define consistent coding styles with EditorConfig to create the .editorconfig file.
Test the changes:
Create a new Windows Forms App (.NET Framework) in Visual Studio (name: WindowsFormsApp1)
Add a class to the project (name: Class1.cs)
In Class1.cs, position the curor inside the curly braces for Class1, then type
ctor
. Next press the Tab key.Resources: