15

I've been having a problem in Visual Studio 2019 where the program enters tabs as four spaces. This is annoying, since I have to hit backspace 4 times to erase an indent, and I need to use arrow keys 4 times to navigate an indent.

This used to work fine, but I had to uninstall and re-install Visual Studio to fix another problem (it kept running old versions of my code and wouldn't run the new version), and ever since then, I haven't been able to get it to work.

I went to settings, and selected 'keep tabs', but it still replaces it with four spaces every time i hit the Tab button. Interestingly, when I start a new line, I can navigate and backspace normally, but if I enter any more tabs, they are replaced with 4 spaces.

I've tried looking around, but I can't seem to find anything that addresses my issue. Can anyone help?

3 Answers 3

12

How to fix problem in Visual Studio with "Keep tabs" not working.

I also had this problem with tabs being converted to spaces.

I checked Tools>Options>Text Editor> (All languasges as well as c/c++) >Tabs> and assured that "Keep tabs" was selected.

After some trouble shooting I found that only one file had this problem (file1.h).

I created a new empty file (file2.h) that worked correctly. I took the statements from file1.h and divided them into small block. Then I moved the blocks to file2 and after each block I tested file2. After a few block suddenly file2.h failed. When I removed the last block from file2 the problem was solved.

My conclusion is, that the source code contained some invivisible code, that made the text editor turn off the "Keep tabs" setting.

I have seen comments about a setting "Use adaptive formatting" that might be relevant. So far I have turned this setting off.

My setup is Visual Studio Community 2019 v 16.4.4 with an Arduino plug in from Visual Micro v 1912 28 2.

/Steen

4
  • I tried opening a new file, and it worked fine, but when I moved any of the code from my program into the new file, the problem re-manifested. I went back to using Pythons IDLE shell, which is working much better. I'll keep in mind that it was probably something about the code itself or how it was formatted that was causing this problem, and I'll probably look into it more at some point in the future. Feb 5, 2020 at 18:35
  • Despite going through the settings and ensuring Keep Tabs was checked, I too had the same problem though with a C# project which I downloaded. I tried removing all invisible control codes by cutting and pasting the code into Textpad, and repasting back... No good. The only thing that worked for me was to paste the code into TextPad, move all code to the left (Shift + Tab), pasting this left aligned code into VS2019, and then retabbing.
    – err1
    Apr 28, 2020 at 9:26
  • 9
    Turning off "Use adaptive formatting" completely solved this frustrating problem for me, thank you!
    – Joosh1337
    May 1, 2020 at 16:47
  • FYI: Joosh1337's answer is the real solution to disable this annoying 'feature'. Dec 24, 2020 at 13:49
11

Disable the checkbox: Tools->Options->Text Editor->Advanced->Use Adaptive Formatting

You will still need to delete the spaces it inserted for you, but will retain tabs afterwards.

2
  • I installed VS2022 today and your advice worked. Prior to this change when I attempted to clear spaces and reformat a block of code with Tabs the VS editor converted the tabs back to spaces. I assume that when "Adaptive Formatting" is in effect the VS editor scans the whole file and applies whatever is the majority indentation style it finds. The source code I had imported from GitHub used spaces for indentation.
    – camelCase
    Dec 9, 2021 at 22:51
  • This is so annoying. There is an option "Use Adaptive Formatting" which you ignore, because it doesn't mean anything to you. And it is enabled by default. Your settings don't work, you google the problem and you find out that that option you ignored was the problem. This is definitely a user interface design issue in Visual Studio. Apr 27, 2022 at 9:02
3

Also note that Visual Studio 2019 now uses and prioritizes .editorconfig files over the Tools -> Options -> Text Editor settings. If you have tried the above answers and still cannot get your indent and tab settings to work, then look for an .editorconfig file anywhere in your project's directory structure.

For instance in my project Angular CLI created one for me when I initialize a new ng project in my .net Web API directory structure. All my *.ts files were not cooperating.

I had to edit the .editorconfig file to look like this in order to get 4 space indent and tab instead of spaces. Once I did that, Ctrl + K + D (reformat) started working again:

# Editor configuration, see https://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = tab
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true

[*.ts]
quote_type = single

[*.md]
max_line_length = off
trim_trailing_whitespace = false
2
  • Thank you so much for this. The inability to control tabs vs. spaces in certain file types, even after overriding ALL file types in Tools/Options, was driving me nuts. Was glad to see I can also turn off that pesky "insert_final_newline" setting for my SQL files. Thanks again
    – JCDrumKing
    Jul 26, 2021 at 16:36
  • This saved the day for me. I couldn't figure out for the the life of me how this could have possibly happened given I hadn't touched a single setting. I happened to generate a new angular component and insert into my project and it must have added (or updated) this file that I didn't even realize was there. Thank you VERY much!!!! Aug 24, 2021 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.