Reputation: 2087
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.
Upvotes: 116
Views: 96700
Reputation: 198
I mostly work with JavaScript, and so the default behavior of my code editor is to insert 2 spaces on tab (indentation using 2 spaces option)
If that is the case for you as well
"[python]": {
"editor.tabSize": 4,
"editor.insertSpaces": true,
}
Will not work, since this tells VS Code that the tab size for Python should be 4, but on tab it does not insert a tab of size 4, instead it inserts spaces (and therefore defaults to 2 spaces inserted per tab) -> to my understanding
It only works when Indent Using Spaces -> 'change view' -> is set to 4
To fix this and make it work as expected, you have to do:
"[python]": {
"editor.tabSize": 4
"editor.insertSpaces": false,
}
Which, to my understanding, tells VS Code to, on tab, insert one tab (and not spaces), and the size of the tab should be 4
Or even try out:
"[python]": {
"editor.insertSpaces": false,
"editor.tabSize": 4,
"editor.detectIndentation": true
}
Upvotes: 0
Reputation: 4165
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.
Upvotes: 237
Reputation: 8572
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.
"[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.
Upvotes: 42
Reputation: 13610
Click File > Preferences > Settings
Type settigns.py
and, click Edit settings.json
in [JSON] section.
"[python]": {
"editor.insertSpaces": true,
"editor.tabSize": 4
}
Upvotes: 9
Reputation: 121
Upvotes: 11
Reputation: 141512
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
Upvotes: 13