如何在 Visual Studio Code 中自动缩进 Python 代码?

如何在 Visual Studio Code 中自动缩进 Python 代码?

问题描述:

我在 Linux 上使用 Visual Studio Code(不是 Visual Studio),但似乎无法找到如何为 Python 启用自动缩进.我查看了所有偏好,在 Google 上花了一些时间,但找不到任何内容.

I'm using Visual Studio Code (not Visual Studio) on Linux and I can't seem to find out how to turn on auto-indentation for Python. I've looked all over preferences, spent some time on Google, and can't find anything.

有人知道怎么做吗?

在 VS Code 中你可以在几个地方设置缩进:

In VS Code you can set the indentation in several places :

  • 常规/工作区设置(底部栏),
  • 用户设置,
  • 语言格式化程序设置.

使用 Python 时,无论您设置什么设置,所有设置都会被 autopep8 语言格式化程序设置的 autopep8Args 值覆盖,该设置的缩进大小为4.

When using Python, no matter what settings you set, all of them are overridden by the autopep8Args value of the autopep8 language formatter setting, which has an indent size of 4.

默认情况下,autopep8 用作 VS Code Python 格式化程序,但还有其他格式,例如 yapf.

By default, autopep8 is used as VS Code Python formatter, but there are others, like yapf.

要更新此格式化程序的缩进大小,请在您的用户设置中搜索python.formatting.autopep8Args"并将其设置为:["--indent-size=2"],

To update the indent size of this formatter, search in your user settings the "python.formatting.autopep8Args" and set it to : ["--indent-size=2"],

"python.formatting.autopep8Args": ["--indent-size=2"],