Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode autoformat going to 2 spaces instead of 4

Whenever I let vscode autoformat my code, it formats to 2 spaces instead of 4, even though apparently the default is 4 spaces, and I have not touched the settings.json

Editor.tabsize also shows 4.

In the bottom right hand corner, its shows spaces: 4, but after autoformatting, it shows spaces: 2, and will revert itself back to 2 no matter how many times I change it to 4.

Anyone know what's going on. This is happening with Ruby, if that matters.

Also, does anyone know of an alternate indenter/autoformatter for VScode?

like image 965
Chris Avatar asked Jan 09 '17 05:01

Chris


People also ask

How do I change the settings JSON in Visual Studio Code?

You can open the settings.json file with the Preferences: Open Settings (JSON) command in the Command Palette (Ctrl+Shift+P). Once the file is open in an editor, delete everything between the two curly braces {} , save the file, and VS Code will go back to using the default values.


2 Answers

I think that you might have a problem with "Auto Detect Indentation". You should try to turn it off and see if that helps.

// The number of spaces a tab is equal to. This setting is overriden based on the file contents when `editor.detectIndentation` is on.
"editor.tabSize": 4,

// When opening a file, `editor.tabSize` and `editor.insertSpaces` will be detected based on the file contents.
"editor.detectIndentation": false
like image 176
juandaco Avatar answered Oct 10 '22 19:10

juandaco


For those who could not use above like me, Here is somewhat GUI guide.

Go to setting by pressing = > CTRL + , (suggested by comment) to open settings or via menue

File->Preferences->Settings   

There you can find both (find tab size and detect indentation) in search box, like I have shown in following image (see the text 'lets play')

detect indentation => uncheck it (turn off)

tab size => Set it to 4

How to ? need to visualize? here it is,

Right click following image and open in new tab see it clearly

enter image description here

like image 37
Sami Avatar answered Oct 10 '22 17:10

Sami