如何在 VisualStudio 代码中设置 Python 语言特定的制表符间距?

使用 VSCode 1.9.0和(donjayamanne) Python 0.5.8扩展 有没有可能提供特定于 Python 的编辑器选项?

或者更一般地说,是否有可能提供特定于语言的制表符间距和替换规则?例如,Python 应该是 tab = 4个空格(替换为空格) ,而 Ruby 应该是 tab = 2个空格(替换)。其他语言往往有自己的观点。但是,我只看到将军

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

选择。

我想可能有一个 "python.editor": { }块或者一个 "python.editor.tabSize"选项,但是我找不到这样的参考,也没有成功地猜到一个工作名称。

66341 次浏览

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/

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.

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

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

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
}