Python 的 VS Code 缩进

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37143985/
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 18:56:47  来源:igfitidea点击:

VS Code indentation for Python

pythonformattingindentationvisual-studio-code

提问by Yahya

How do I enable indentation in VS Code?

如何在 VS Code 中启用缩进?

I'm trying to learn Python (newbie in programming) and need auto-indentation. It worked with the first version I tried, but it doesn't indent after a colon (:) anymore. How can I configure it to automatically indent?

我正在尝试学习 Python(编程新手)并且需要自动缩进。它适用于我尝试的第一个版本,但它不再在冒号 ( :)后缩进。如何将其配置为自动缩进?

采纳答案by Danny

You can install VSCode python extension which will provide intellisense, auto-completion, code formatting, and debugging. Here is more information on the python extension, here

您可以安装 VSCode python 扩展,它将提供智能感知、自动完成、代码格式化和调试。这是有关 python 扩展的更多信息, here

回答by Mohamed Allal

As said there is the python extension which now do it out of the box, but still don't do a great job, and example is when you copy and past a whole block into a function or so, it just indent the first line, that's not a good behavior. here are two good helpful solutions:

如前所述,现在有 python 扩展,它现在开箱即用,但仍然没有做得很好,例如,当您将整个块复制并传递到一个函数左右时,它只是缩进第一行,这不是一个好的行为。这里有两个很好的有用的解决方案:

  • indent a whole block manually: select the whole blockthen click TAB, if you want to indent backwardyou do it with SHIFT+TAB. That's it, i think that can be useful in several places.
  • python auto indent extension(https://marketplace.visualstudio.com/items?itemName=hyesun.py-paste-indent), it solve the problem when pasting. just see how it work in the link. Now about setting it up, you need to set just one keybinding. for the command "pyPasteIndent.pasteAndIndent"provided by the extension. Once done you will have your own shortcut to past and indent automatically (i have set it to ALT+P)

    Here HOW: - CTRL+SHIFT+Pto open command pallet, then write "key"*, choose *"open keyboard shortcut", then the keybinding page open, which it's the nice interface for the **keybindings.json. You can open keybindings.jsonthe same way and by choosing "open keyboard shortcut file"(in place of just "open keyboard shortcut"). Give it a look if never have. But here i will go with the nice Interface, know also that you can open that Going File>Preference>Keyboard Shortcut.

  • 手动缩进整个块选择整个块然后单击TAB,如果要向后缩进,请使用SHIFT+ 进行TAB。就是这样,我认为这可以在几个地方有用。
  • python自动缩进扩展https://marketplace.visualstudio.com/items?itemName=hyesun.py-paste-indent),它解决了粘贴时的问题。看看它是如何在链接中工作的。现在关于设置它,您只需要设置一个 keybinding。对于扩展提供的命令“pyPasteIndent.pasteAndIndent”。完成后,您将拥有自己的过去和自动缩进的快捷方式(我已将其设置为 ALT+P)

    这里 HOW: - CTRL+ SHIFT+P打开命令托盘,然后写"key"*,选择 *"open keyboard shortcut" ,然后键绑定页面打开,这是 **keybindings.json 的漂亮界面。您可以以相同的方式打开keybindings.json并选择“打开键盘快捷键文件”(而不是“打开键盘快捷键”)。如果从来没有,就看看吧。但是在这里我将使用漂亮的界面,也知道您可以打开 Going File>Preference>Keyboard Shortcut

In the keybinding window, in the search bar, past pyPasteIndent.pasteAndIndent, then click +button to add the shortcut and create the keybinding.

在键绑定窗口中,在搜索栏中,通过pyPasteIndent.pasteAndIndent,然后单击+按钮添加快捷方式并创建键绑定。

The image bellow show well how it's done:

下图很好地展示了它是如何完成的:

enter image description here

在此处输入图片说明

回答by Malte

I faced similar issues switching from PyCharm. The Python Indentextension, which is available in the Visual Studio Code marketplace, works perfectly fine for me.

我从 PyCharm 切换时遇到了类似的问题。Visual Studio Code 市场中提供的Python Indent扩展对我来说非常好。

回答by Marjan Radfar

I installed this extension: Python Indent. It works very well.

我安装了这个扩展:Python Indent。它运作良好。

回答by Sand1512

I faced similar issues while editing. Select the lines of code you wish to intend and press CTRL+]in windows or CMD+]in mac.
You can change the indent size in settings. Search for tab sizein settings. I use 2 btw.

我在编辑时遇到了类似的问题。选择的代码行,你要打算并按CTRL+]在Windows或CMD+]在Mac。
您可以在设置中更改缩进大小。tab size在设置中搜索。我用 2 顺便说一句。

回答by Prayag Bhatia

Simple solution ! Click tab size on bottom right corner and choose 'convert indentation to tabs' or 'convert indentation to spaces' as per your requirement.

简单的解决方案!单击右下角的制表符大小,然后根据您的要求选择“将缩进转换为制表符”或“将缩进转换为空格”。