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

PythonConfigurationVisual Studio-CodeCode FormattingVscode Settings

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

> 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/

Solution 3 - Python

For anyone still facing a problem, and 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 decide themselves how to indent code. Some extensions do not choose indentation but others do, and what's worse is that vscode seem to "remember" indentation within a file. So having had a wrong indentation once, one can experience that the suggested answer does not work within the language. This is because vscode will also try to "detect indentation". And this causes a major problem if the auto detection believes it has to indent in a way that does not conform with your user settings. Luckily the fix is simply turning this off by adding "editor.detectIndentation" : false to the global or language 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 setting"
  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.

Solution 4 - Python

  1. Editor: Detect Indentation = false (default = true)
  2. Editor: Insert Spaces = true (default)
  3. Editor: Tab Size = 4 (default)

Solution 5 - Python

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
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmichael_teterView Question on Stackoverflow
Solution 1 - Pythonzeljko_aView Answer on Stackoverflow
Solution 2 - PythonShaun LuttinView Answer on Stackoverflow
Solution 3 - PythonOliverView Answer on Stackoverflow
Solution 4 - PythonBenedito CarneiroView Answer on Stackoverflow
Solution 5 - PythonLukasz DynowskiView Answer on Stackoverflow