1

I'm having difficulty with tab size in VSCode. For some YAML files I'm getting 2-space indentation while others are getting 4-space indentation. I'd like to always use 2-space indentation.

The below images show what the files look like when open in VSCode. Note that the vertical lines indicating indentation depth are different. This points to VSCode recognizing the indentation differently for the two files.

Some look like this:

enter image description here

Some look like this:

enter image description here

The yaml-specific settings in my user settings are:

    "yaml.completion": false,
    "yaml.format.enable": true,
    "[yaml]": {
        "editor.defaultFormatter": "redhat.vscode-yaml",
        "editor.tabSize": 2
    },

It appears to me that VSCode is actually recognizing the different indentation in the two files and is respecting that rather than respecting my tabSize: 2 setting. Does anyone know how to get 2 spaces to be used for all YAML files?

Vorticity
  • 4,582
  • 4
  • 32
  • 49
  • 1
    Is it possible you created the 4-space files before you set that setting? Or do they come from elsewhere? You might just need to reindent them. – wjandrea Apr 11 '23 at 17:44
  • Related to the above point on reindentation: [In VS Code, How do I format a document (.cpp) so that it converts indentation with N spaces to indentation with M spaces?](/q/75954165/11107541) – starball Apr 11 '23 at 17:45
  • You're right, they were created by someone else. I redid the indentation, though. When I saved the file it reindented to 4-space indentation. – Vorticity Apr 11 '23 at 17:45
  • hm. what if you redo the indentation and then close that editor tab and then reopen the file and try saving then? or restart VS Code? – starball Apr 11 '23 at 17:46
  • @user that actually does fix the problem for one file. I'd like them all to reformat to 2 spaces, but maybe I'll just have to script that. – Vorticity Apr 11 '23 at 17:48
  • see [How to execute command across multiple files in VS Code?](https://stackoverflow.com/q/47652442/11107541) – starball Apr 11 '23 at 17:50
  • 1
    Thanks @user, this looks handy. I'll give it a shot for some other stuff. Some language you used, though, led me to the solution. I turned off "detect indentation" and it fixed my problem! – Vorticity Apr 11 '23 at 18:01

1 Answers1

0

Some language in the comments led me to the solution. There is a setting to "detect indentation". Disabling that setting made VSCode respect my indentation settings.

The setting to disable is Editor: Detect Indentation.

Vorticity
  • 4,582
  • 4
  • 32
  • 49