How to change tab size into 4, instead of the standard 2 spaces?

Hello.

I’m new to PlatformIO and VSC for that matter, I really don’t like and I don’t understand why Arduino have chosen to change the size of tab from the universal 4 spaces into 2 spaces.
I like to change the tab size in PlatformIO, but if I go into settings in VSC I can change the tab size and it is set to 4, so where would I find that setting for PlatformIO?
Is there a preference.txt file such as in the Arduino IDE and if so where is it found?

Regards

Since it’s a space-based indent (rather than a tab based one)… you need to apply the changes after making them.

i.e. After changing this setting (click on it to change)…

… you then need to re-format your code (right click → Format Document, or Format Document With... if VSCode is being stupid and listing multiple formatters)

image

I have the same question. How can I change the default tab size to 2 spaces do I don’t need to change this for every project?

I don’t see any settings for this in the platformio extension.

  1. Open VS Code settings

image

  1. Enter “tab” in the search bar
  2. Change “Tab Size” to 4:

I think the problem is not understood here.
If I open a non-platfomio project in vscode, I get 4 spaces when I hit tab, since Editor: Tab Size is set to 4 already.
However, when I hit tab in a platformio project, I get 2 spaces.
Can anyone help me please ?

Does this also apply on a fresh new PlatformIO project in a fresh new VS Code Workspace?

Yes, thanks for your suggestion. I just tried that and it’s still making 2 spaces.

otherwise, it makes 4 spaces. here is a regular text file

or here is an arduino platform project

I just can not get platfoimio to make 4 space tabs in vscode :frowning:

Do you have file type specific indention settings?

No it wasn’t. But your suggestion helped me to figure out what the problem is.

The culprit is the Detect Indentation setting.

Controls whether [Editor: Tab Size] and [Editor: Insert Spaces] will be automatically detected when a file is opened based on the file contents.

I turned off this setting and everything went back to normal.

This is due to the reason that when a new project is made (Arduino based in my case) it starts with the 2 spaced following code:

#include <Arduino.h>

// put function declarations here:
int myFunction(int, int);

void setup() {
  // put your setup code here, to run once:
  int result = myFunction(2, 3);
}

void loop() {
  // put your main code here, to run repeatedly:
}

// put function definitions here:
int myFunction(int x, int y) {
  return x + y;
}

And the Detect Indentation setting forces the project to be in 2 space tab from this point on, even if the tab space is set like "editor.tabSize": 4

Does anyone know where the above initial code comes from or how can I change it?

Thanks.

Good catch!

Unfortunately, this code is built-in and cannot be changed.

I started to change the way how I create a new Project:

  • Create a new project folder
  • Open a terminal window
  • Command “pio project init -b <board-id-here> --ide vscode
  • Command “code .
  • Create ./src/main.cpp file

I also have configured this user-snippet

{
	"Arduino default code": {
		"prefix": "adc",
		"body": [
			"#include <Arduino.h>",
			"",
			"void setup() {",
			"    Serial.begin(115200);",
			"    Serial.println(\"Hello World\");",
			"}",
			"",
			"void loop() {",
			"}"
		]
	}
}

This allows me to simply type “adc” to have my own “default” code.

All these steps sounds a bit cumbersome but in the end it is faster than using PIO Home / Projects / Create new Project …

I will try this.
Thanks a lot.