VS Code indentation for Python

大憨熊 提交于 2019-11-29 06:47:55
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

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:

I faced similar issues while editing. Select the lines of code you wish to intend and press CTRL+] in windows or CMD+] in mac.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!