PyCharm: Automatically align in-line comments
Asked Answered
F

2

20

I use PyCharm 2016.2 to write my Python programs. Some of my in-line comments look a little messe:

code    # comment
code  # comment
more code  # comment

Is there a way to have PyCharm automatically align all in-line comments in a section or file on a vertical line. So it looks like this:

code       # comment
code       # comment
more code  # comment

Related to that: I tried doing it manually, but PyCharm sometimes messes up my indentation of comments, e.g., when copying code. How can I change that?

Ferminafermion answered 6/11, 2016 at 16:37 Comment(4)
How did you align them in the first place? Regardless of that you should try to avoid inline comments. It is better to use a comment above multiple lines of code explaining what they do as a whole. Look up PEP8.Kathrynekathy
I insert additional tabs to manually align the comments. Ok, I will keep that in mind and try to use fewer inline comments. In my opinion, there sometimes still helpful, though.Ferminafermion
How to add such inline comments in Pycharm ? Everytime I do Shift+3, it changes the whole line of code into a line commentBiddie
this answer might help youOrianna
V
0

There is no way to do this in PyCharm. There is an open issue that you can vote for: https://youtrack.jetbrains.com/issue/PY-71097/Annoying-removal-of-spaces-before-aligned-inline-comments

Vassily answered 9/8 at 12:39 Comment(0)
M
-3

In Pycharm, do this while your script is open: Click on Code, then click on Reformat Code (or use the shortcut). I tried it on your example and it worked.

Note: If you only want to reformat part of your script, first run your mouse over the desired portion of the script to select it. Then follow above procedure.

Mickey answered 15/3, 2019 at 15:28 Comment(1)
This is exactly what doesnt work; Atleast on the default settings.Omnivore

© 2022 - 2024 — McMap. All rights reserved.