Visual Studio 2019 ignoring tab preference
Asked Answered
W

4

18

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.

Wysocki answered 20/1, 2020 at 3:1 Comment(2)
Do you have any other addins, like Resharper?Jenkins
No, I am using VS2019 out-of-the-box (save for, of course, user preferences)Wysocki
W
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

Wysocki answered 21/1, 2020 at 0:7 Comment(4)
Thanks for the answer. It seems largely undocumented as of right now. I hope this changes for the future.Blower
9 months later (September 2020) and this new feature is still undocumented, as is the "Responsive code completion" feature. The Help button in VS is useless. C'mon Microsoft, you're better than this.Levelheaded
Doesn't have any effect for me.While
Had the same issue with VS 2022 in CS files. Disable "Use adaptive formatting" helped. ThxRemex
F
9

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
Flivver answered 23/3, 2021 at 20:47 Comment(1)
That's fantastic, I haven't seen this documented much anywhere else. Thank you.Batiste
B
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.

Batiste answered 26/5, 2021 at 17:22 Comment(4)
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.Batiste
Standards are great, everyone should have one.Batiste
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!Yovonnda
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.Batiste
I
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

Ironwork answered 6/1, 2021 at 14:17 Comment(1)
This is what I needed, it was the All Languages -> Tabs option, only you know... spaces.Vernal

© 2022 - 2024 — McMap. All rights reserved.