19

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?

Olian04
  • 6,480
  • 2
  • 27
  • 54
stefanbschneider
  • 5,460
  • 8
  • 50
  • 88
  • 1
    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. – Nick is tired Nov 06 '16 at 16:39
  • 1
    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. – stefanbschneider Nov 06 '16 at 16:50
  • How to add such inline comments in Pycharm ? Everytime I do Shift+3, it changes the whole line of code into a line comment – Kanmani May 01 '18 at 04:25
  • [this answer](https://stackoverflow.com/a/52560760/2532408) might help you – Marcel Wilson Jul 08 '21 at 19:33

1 Answers1

-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.

geekandglitter
  • 342
  • 2
  • 15