'Python Autocompletion in VS Code

How can I add a colon automatically at the end of "def", "for" and "if" statements in Python for Visual Studio Code just like in Spyder?



Solution 1:[1]

You can use code snippets to insert code with the colon included. Otherwise you will need to create an extension which implements formatting-on-type to do this for you.

Solution 2:[2]

Switching from PyCharm to VSCode, I have finally found PyColonize

Showcase

It adds these shortcuts:

  • Shift + Enter — Insert colon at the end of line and continue on the same line

  • Ctrl/CMD + Enter — Insert colon at the end of line and continue on the new line

  • Ctrl/CMD + Alt + Enter — Insert colon and stay at the same position

By default shortcut will work only with Python files. If you want to mimic this behaviour in other languages as well, but without a colon:

  1. Open keybindings.json (Ctrl/CMD + Shift + P, type >Open Keyboard Shortcuts (JSON))
  2. Insert the following:
      {
        "key": "cmd+enter",
        "command": "editor.action.insertLineAfter",
        "when": "editorTextFocus && !editorReadonly && editorLangId != 'python'"
      }

For semicolons, see colonize

If you're switching to VSCode from PyCharm, take a look at pycharm-to-vscode-transition repository I've created to help smoothen such transition

Solution 3:[3]

go to extensions and search for pylint, then install pylint extension

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brett Cannon
Solution 2
Solution 3 akinlex