Who messed up Visual Studio tab indent level?

After one of latest Visual Studio 2017 I noticed that tab length is incorrect in C# editor. I went through options and confirmed that tab size for C# was set to four spaces. But still for some reason editor used two as tab size. There have been some small changes how Visual Studio 2017 prioritizes settings and here is how to solve the mess.

New kid on the block – .editorconfig file

As it turns out there is something called .editorconfig file and from now on it overrides what is set in options dialog. Yes, you can set tab size in options dialog and then have somewhere in file tree .editorconfig file that makes the final rules. I found one that was located in my projects root directory meaning that there should be no understandable reason for Visual Studio to go with this file but still…

EditorConfig is actually standard shared between different code editors. From their homepage we can get the first idea what it is all about.

EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs. The EditorConfig project consists of a file format for defining coding styles and a collection of text editor plugins that enable editors to read the file format and adhere to defined styles. EditorConfig files are easily readable and they work nicely with version control systems.

Here is the .editorconfig file I found from projects root folder.

# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]

# change these settings to your own preference
indent_style = space
indent_size = 2

# we recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[{package,bower}.json]
indent_style = space
indent_size = 2

The first settings block marked with [*] sets defaults to all file types opened in editor. After this come blocks that override required settings for different file types. Notice that by default tab size is two for all files.

Restoring tab size

To restore previous defaults there are two options. It’s possible to set default indent size to four and force it to all file types by default (except those that have settings block). The other option is to add block for C# to end of settings file.

[*.cs]
indent_style = space
indent_size = 4

It’s also possible to delete the file and then there’s nothing to override Visual Studio options. I prefer to leave the file where it is and just make my own defaults so there’s no danger that somehow the file comes back and screws up editor settings again.

Wrapping up

It’s okay that Visual Studio team jumps to different initiations by community but it cannot come as a silent surprise through update. In my opinion there must be setting in Visual Studio to turn support for .editorconfig on and off. Currently it came like bad surprise and first I thought that some extension has screwed up something. On the other hand it’s good to have generalized settings applied to code editors in Visual Studio and VS Code. I think if I have to work with both of these then generic editor settings would be good thing.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    4 thoughts on “Who messed up Visual Studio tab indent level?

    • Pingback:The Morning Brew - Chris Alcock » The Morning Brew #2484

    • December 12, 2017 at 10:31 am
      Permalink

      you could also disable ‘Follow project coding conventions’ in Options > Text Editor > General

    • December 12, 2017 at 12:12 pm
      Permalink

      EditorConfig support was added in VS2017 release version, not added through one of the recent updates. If you’ve only just started noticing its effects, then perhaps the file you found was only recently added, or perhaps something was fixed in an update, but I’ve been using it since 2017 first came out.

    • May 14, 2019 at 7:06 pm
      Permalink

      Thank You!! I had the same issue in Visual Studio Code. Now I have my 4 character tabs back.

    Leave a Reply

    Your email address will not be published. Required fields are marked *