How to set Python language specific tab spacing in Visual Studio Code?
Asked Answered
H

5

108

Using VSCode 1.9.0 with the (donjayamanne) Python 0.5.8 extension, is it possible to provide Python specific editor options?

Or more generally speaking, is it possible to provide language specific tab spacing and replacement rules? For example, Python should be tab=4 spaces (replaced as spaces), and Ruby should be tab=2 spaces (replaced). Other languages tend to have their own opinions. However, I only see the general

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

options.

I thought perhaps there was a "python.editor": { } block or perhaps a "python.editor.tabSize" option, but I can't find reference to such, nor have I successfully guessed a working name.

Harbour answered 8/2, 2017 at 16:33 Comment(2)
How do I auto-indent Python code in Visual Studio Code? #35531580Depression
This fixed issue - #57751380Chauncey
L
226

I had the same problem today.
This is how I fixed it. Add this lines in setting.json in VSCode:

"[python]": {
  "editor.insertSpaces": true,
  "editor.tabSize": 4
}

It works like a charm.

Londonderry answered 7/2, 2018 at 16:52 Comment(6)
It also works for the other files format: [javascript], [html], [json], [tsv]....Vercingetorix
code.visualstudio.com/docs/getstarted/…Avril
As Oliver explains in his answer, it may be helpful to include "editor.detectIndentation" : false in the [python] block. See his answer for details.Harbour
Note: I had to restart vscode in order for the change to take effect.Eshelman
I added two of the above mentioned configurations, one for Python (spaces) and one for C (tabs), but unfortunately, the editor keeps the previous set setting and does not use the language specific one. Do I have to set an additional setting?Baywood
I had to turn Editor: Detect Indentation settings to False for my settings.json to work.Rhoden
B
39

For all finding that the default answer did not solve your problem, here is a method based on this discussion/issue on the vscode github.

The problem most likely stems from the fact that vscode and their extensions themselves decide how to indent code. Some extensions do not choose indentation but others do, and what's worse is that vscode seem to "remember" indentation. So having had a wrong indentation once, one can experience that the suggested answer does not solve your issue as the incorrect indentation was detected in another file. This can cause a lot of frustration, if the indentation detected does not conform with your user settings (even after following the default answer). Luckily the fix is simply turning this off by adding "editor.detectIndentation" : false to the global or language specific settings in addition to the values specified by the accepted answer.

step by step guide

  1. Use ctrl+shift+p and write "settings"
  2. Click "Open User Settings (JSON)"
  3. Add the following to your json script (remember fields need to end with a comma, if it is followed by another field)
"[python]": {
        "editor.detectIndentation" : false,
        "editor.insertSpaces": true,
        "editor.tabSize": 4   
    }

changing the number "4" to the number of spaces you wish to use for indentation.

Bondie answered 14/5, 2021 at 15:57 Comment(1)
Since the key difference of your solution compared to the accepted answer is the inclusion of the editor.detectIndentation option, perhaps it would have been better to amend the accepted answer with your additional line? That said, your explanation text is new and informative.Harbour
M
13

Python should be tab=4 spaces (replaced as spaces), and Ruby should be tab=2 spaces...

Install the editor config plugin.

ext install EditorConfig

Add an .editorconfig file to your project root with Python and Ruby specific settings:

[*.py]
indent_style = space
indent_size = 4

[*.rb]
indent_style = space
indent_size = 2

These are other supported properties:

tab_width
end_of_line
insert_final_newline
trim_trailing_whitespace

See also:

https://github.com/editorconfig/editorconfig-vscode

http://editorconfig.org/

Marivelmariya answered 9/2, 2017 at 3:16 Comment(0)
S
11
  1. Editor: Detect Indentation = false (default = true)
  2. Editor: Insert Spaces = true (default)
  3. Editor: Tab Size = 4 (default)
Salesperson answered 8/2, 2019 at 19:31 Comment(2)
This worked for me just now! The top voted answer with [python] setting above did not work for me, as these settings seem to override otherwise in VS Code 1.54.1Gynaecomastia
Thanks for the default info, where did you find it?Lewis
I
9

1st Locate Settings

Click File > Preferences > Settings

enter image description here

2nd Edit Settings

Type settigns.py and, click Edit settings.json in [JSON] section.

enter image description here

3rd Add pyton config

"[python]": {
  "editor.insertSpaces": true,
  "editor.tabSize": 4
}
Interlope answered 28/6, 2021 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.