Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Change VSCode To Indent 4 Spaces Instead Of Default 2?

I have applied the below settings in VS Code to get 4 spaces indentation.
But always when I open a new file, it switches back to 2 in the right-bottom corner.

If I click in the right-bottom corner and change the setting back to 4,
VSCode will still change back to 2 and still apply it with the 2-space auto-indent.
Alt+Shift+F

What am I missing?

enter image description here

like image 685
alebo611 Avatar asked Mar 08 '18 06:03

alebo611


People also ask

How do you set a tab to 4 spaces in Visual Studio code?

Type “Indentation” into the search field then head to the “Editor: Tab Size” section. Replace the default space number with your preferred one: Your setting will be applied and reflected immediately. If this doesn't happen (it's a little lag sometimes), just reload or restart your VS Code.


2 Answers

Bit of an late answer. But just got the same issue solved...

Multiple things are able to control this. It also has taken me quite a bit of experimentation to get it corrected. For me point 3 below was the final trick to make it work. Before that, I noticed the editor loading with 4, but jumping back to 2 spaces. Now it stays at 4.

Some things to check:

1: VS Code configuration (Settings & Workspace, you can set these for system wide configuration or just for the current Workspace): Check whether you have set:

"editor.tabSize": 4, "editor.insertSpaces": true, "editor.detectIndentation": false 

And language specific settings (optional):

"[javascript]": {     "editor.tabSize": 4 }, "[typescript]": {     "editor.tabSize": 4 } 


2: Are there any Extensions that could influence the indentation -> people have reported JS-CSS-HTML to also configure the setting.


3: Is there a .editorconfig file in your workspace? If so, check the settings over there. Angular creates one for example and configures the indent_size:

# Editor configuration, see http://editorconfig.org root = true  [*] charset = utf-8 indent_style = space indent_size = 4 insert_final_newline = true trim_trailing_whitespace = true  [*.md] max_line_length = off trim_trailing_whitespace = false 

Most answers focussed point 1 which, but for me the last step was important to make VS Code work properly.

This Stack Overflow handles all of the above in different answers: Visual Studio Code: format is not using indent settings

like image 103
Oskar H. Avatar answered Oct 11 '22 12:10

Oskar H.


I fixed it in the VisualStudio settings (1.31)

Go to: settings > workspace settings > Text editor

uncheck 'Detect Indentation' to stick to your default setting.

like image 24
Thomas Avatar answered Oct 11 '22 14:10

Thomas