16

In Jetbrains Pycharm (Professional, 2016.1), is there a way (built in or third-party) to modify the code style in python to align consecutive lines by an operator, particularly an assignment one? For example, I would like

var1 = 5
variable2 = 10
varbl3 = 15

to be reformatted to

var1      = 5
variable2 = 10
varbl3    = 15

I am aware of several packages that let me do this manually with an external command (I am currently using Front-End Alignment, but String Manupulation also works decently), but it precludes me from also using code reformatting (which I otherwise find very useful) on the same piece of code.

I am aware this does not follow PEP-8 standards, and I don't wish to have a debate about the merits/demerits of aligning code. Is there a way to allow the reformat code option to automatically take care of this for me?

Ben Bartlett
  • 322
  • 4
  • 14

1 Answers1

13

To my knowledge, there isn't an automatic way.

That said, something I found very helpful is "Add multiline carets on double ⌥ (Option key) with arrow keys" (found in General>Smart Keys) along with "Move Caret to Next Word".

  1. Put the cursor on line 1, double tap ⌥ and hold then press the down arrow the number of lines to edit.

  2. Let go of ⌥ (to get Pycharm out of 'add more cursors mode') then using "Move Caret to Next Word" ⌥→ move the cursors just before the = on each line.

  3. Add a bunch of spaces till all the equal signs are past the alignment point.

  4. Put the cursors back to the start of the line to get them aligned again.

  5. Move them to the alignment point.

  6. Shift ⌥ → to highlight the space up to the =.

  7. Then backspace to line everything up.

** I'm on mac, so the keys are going to be different for other OS.

Marcel Wilson
  • 3,842
  • 1
  • 26
  • 55