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

2

8

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.

Portamento answered 22/5, 2018 at 22:3 Comment(0)
G
0

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
Gono answered 14/11, 2018 at 17:29 Comment(0)
P
0

Install the ms-python Extension.

This offers linting, intellisense, and code-formatting. I'm not sure if it's exactly like IDLE but I've been using it for Python development for several months and it does right by me.

  1. Click this button enter image description here

  2. The enter Python in the search box and press enter

  3. The Extension published by Microsoft is the one I recommend
Puccini answered 22/5, 2018 at 22:12 Comment(0)
G
0

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
Gono answered 14/11, 2018 at 17:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.