17

I was programming in VS2019 this morning before going about my day. When I sat down in the evening to keep programming, I realized that it has suddenly been placing spaces instead of tabs! My preferences are set for every language to place tabs instead of spaces, and has been doing so in previous days. I updated VS to make sure, but the problem persists - regardless of my preference settings, out of nowhere spaces are being placed instead of tabs.

PS: I am aware that this question has already been asked here, but their solution was to update VS - which did not change anything for me.

Khide Hoskins
  • 397
  • 2
  • 8

4 Answers4

23

I found the solution! In the options, go to Text Editor > Advanced and turn off "Use adaptive formatting". It was overwriting my preferences based off of the already-existing formatting of a given file

Khide Hoskins
  • 397
  • 2
  • 8
8

Maybe this helps because I had the same problem, checked all the usual places mentioned in the answers which is Tools -> Options -> Editor -> yadda yadda ... but nothing helped.

Now I found the solution hidden in some comment. The project I downloaded from git had a file named .editorconfig in scope, and this was overriding the settings that you can set in the Tools menu.

Look for a line

indent_style = spaces

and change it to

indent_style = tab
Devolus
  • 21,661
  • 13
  • 66
  • 113
5

This is very frustrating - I found that even though I had the editor config setup correctly, VS2019 would spontaneously insert tabs into the file.

I vastly prefer tabs, but at my current job they use spaces, so you gotta go with the concensus.

Much to their dismay, my files would be checked in with tabs, and of course Git would natter at you about changes.

It turns out, per this thread: https://developercommunity.visualstudio.com/t/visual-studio-20194-c-insert-spaces-instead-of-tab/847853 Visual Studio 2019 will essentially ignore your setting and decide, based on the contents of the file, to spontaneously switch to what it thinks is correct.

This cannot be changed apparently:

This is the Adaptive Formatting behavior in Visual Studio: the Editor heuristically determines if the current file should use Tabs or Spaces for indentation.

We don’t currently have an option to disable adaptive white space, but if folks feel that’s important, we can add it.

LOTS of people complained, but then you get the (standard for now) disclaimer of "we work on the squeeky wheel".

So we're on our own.

So you can try turning it off, but VS2019 will still maintain the file in what it thinks is proper - even if there is only ONE TAB in the file.

Therefor, the only complete fix, again straight from Microsoft:

Third, if you use .editorconfig in your code repo, we will ALWAYS honor those settings. It’s not a requirement to use .editorconfig, but anyone who is particular interested in maintaining a coding style should know that our of the VS guiding principles is to never believe we’re “smart enough” to override .editorconfig.

My full editor config:

[*]
indent_style = space
indent_size = 3
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

Note the "end_of_line" ... this also fixes problems with WSL, as otherwise Git may change your line endings to CRLF in .sh files ... which doesn't work with WSL.

Along with this bonus solution, set your .gitattributes file:

* text=auto eol=lf

*.{cmd,[cC][mM][dD]} text eol=crlf
*.{bat,[bB][aA][tT]} text eol=crlf
*.cs                  text eol=crlf
*.xaml                text eol=crlf
*.csproj              text eol=crlf
*.njsproj             text eol=crlf
*.pyproj              text eol=crlf
*.sln                 text eol=crlf

So you can kill two birds with one stone.

J. Gwinner
  • 931
  • 10
  • 15
  • I recently ran into a project that *requires* tabs, instead of spaces [as the rest of the company mandates]. So, the .editorconfig is about the only thing to save you! That way each project can run their own standards. – J. Gwinner Jan 18 '22 at 17:28
  • 1
    Standards are great, everyone should have one. – J. Gwinner Jan 18 '22 at 17:28
  • 1
    Thank you for showing me that an editorconfig *anywhere* above where your solution is in the repository can affect this. This was MADDENING on how an example was just doing weird shit when I tried to edit it, because of some global default, inapplicable to the language I was using! – Kevin Anderson Feb 11 '22 at 16:03
  • Glad I could help Kevin! Yea, this kind of behavior along with "adaptive formatting" in Visual Studio (not VSCode) is nuts. Also, if you ARE using Visual Studio Code, I just found out it ignores the .editorconfig, but there is an extension (of course) that fixes this. – J. Gwinner Feb 13 '22 at 02:14
3

Try all this (solved my problem):

Tools -> Options -> Text Editor -> Advanced -> turn off "Use adaptive formatting"

Tools -> Options -> Text Editor -> All Languages -> Tabs -> Keep tabs

Tools -> Options -> Text Editor -> C# -> Tabs -> Keep tabs

Jan Macháček
  • 612
  • 7
  • 11
  • This is what I needed, it was the `All Languages -> Tabs` option, only you know... spaces. – Aaron Mar 24 '22 at 03:29