VS Code indentation for Python

2019-01-25 16:37发布

How do I enable indentation in 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?

2条回答
对你真心纯属浪费
2楼-- · 2019-01-25 16:56

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:

  • indent a whole block manually: select the whole block then click TAB , if you want to indent backward you 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+P to 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.json the 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.

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

The image bellow show well how it's done:

enter image description here

查看更多
祖国的老花朵
3楼-- · 2019-01-25 17:09

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

查看更多
登录 后发表回答