Search code examples
pythonindentationpython-idlevisual-studio-code

How to change the behavior of indenting the new line after comma in Visual Studio Code?


I am a fresh python programmer. I started using VS Code because of its built-in terminal. (Before that I was using IDLE as an editor.)

My problem is, when I writing the code below in IDLE, all I need to do is to press enter to indent the new line to align with the previous variable.

someFunction(longVariable,
             longString,
             longWhatever)

In VS Code, it does this when I press enter:

someFunction(longVariable,
longString,
longWhatever)

Funny thing is when I align the "longString" by myself, It goes to the exact point where I want it to be as I press enter after comma. Like this:

someFunction(longVariable,
             longString,
             longWhatever)

How can I make it behave like IDLE? I use it a lot.


Solution

  • In addition to installing the ms-python extension that @TheDude suggested, I had to install a formatter. The options are autopep8, YAPF, and Black. Here are the install commands (you only need one of them, I chose YAPF because I was getting weird results with autopep8):

    pip install yapf
    
    pip install autopep8
    
    pip install black
    

    Now, I could not get it working that it would auto-format when I return a line. There is a User Setting parameter called editor.formatOnType which I thought would take care of this, but it seems to have no effect. BUT, there is also a User Setting parameter called editor.formatOnSave that auto-formats your file every time you save it. If you change this in your user settings "editor.formatOnSave": true then every time you save your file, it gets auto-formatted.

    You can also use the auto-formatting hotkeys and it will have the same effect:

    • Windows: Shift + Alt + F
    • Mac: Shift + Option + F
    • Ubuntu: Ctrl + Shift + I