如何在 Visual Studio Code 中设置 Python 语言特定的选项卡间距?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42118651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:16:14  来源:igfitidea点击:

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

pythonconfigurationvisual-studio-codecode-formattingvscode-settings

提问by Michael Teter

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

将 VSCode 1.9.0 与(donjayamanne)Python 0.5.8 扩展一起使用,是否可以提供特定于 Python 的编辑器选项?

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

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

"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.editor": { }块或一个"python.editor.tabSize"选项,但我找不到这样的参考,也没有成功猜到一个工作名称。

回答by zeljko_a

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

我今天遇到了同样的问题。
这就是我修复它的方式。在 VSCode 的 setting.json 中添加以下几行:

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

It works like a charm.

它就像一个魅力。

回答by Shaun Luttin

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

Python应该是tab=4个空格(替换为空格),Ruby应该是tab=2个空格...

Install the editor config plugin.

安装编辑器配置插件。

ext install EditorConfig

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

.editorconfig使用 Python 和 Ruby 特定设置将文件添加到您的项目根目录:

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

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

http://editorconfig.org/

http://editorconfig.org/

回答by Benedito Carneiro

  1. Editor: Detect Indentation = false (default = true)
  2. Editor: Insert Spaces = true (default)
  3. Editor: Tab Size = 4 (default)
  1. 编辑器:检测缩进 = false(默认 = true)
  2. 编辑器:插入空格 = true(默认)
  3. 编辑器:标签大小 = 4(默认)