4

How do I make "Format on Save" use the same indentation as when I'm editing?

E.g. after writing the following snippet (lines are auto-indented with 4-spaces):

Edit with 4-space indents

However, when I enable "Format on Save", the saved file uses 2-spaces for indentation:

Saved with 2-space indents

8

To answer my own question...

I'm using the Prettier extension, and it had the number of spaces per tab set to 2.

With the "Format On Save" option enabled, "editor.formatOnSave": true, VS Code uses the configured code formatter, which is Prettier in my case. The default tab width used by Prettier is 2.

After setting it to 4, the file is now saved with 4 spaces. The Settings.json is updated with:

"prettier.tabWidth": 4,

I thought Prettier would apply to both editing and saving. Evidently, Prettier formatting is only being applied during save. I also notice that "Format Selection", Ctrl-K Ctrl-F, is not using Prettier tab spacing either.

It would be nice if I could set the Prettier tab width to match the editor setting, so that I only have to change this in one place. Is that possible?

Matt.

1
  • Prettier is doing his job when saving, formatting to 4 spaces, but while im typing vs code force the 2 spaces and its pretty annoying get a pre format in 2 spaces. Dont know how to change it. May 9 '21 at 13:23

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.