Align key-value pair in PyCharm

3.5k views Asked by At

Is it possible to set Align key-value pair in Pycharm Code Style? I'm looking for something that will reformat:

nickname = models.CharField(unique=True, max_length=24)
biography = models.TextField(default=BIO_DEFAULT_STRING)
avatar = models.ImageField(upload_to='members/avatars/')

to:

nickname  = models.CharField(unique=True, max_length=24)
biography = models.TextField(default=BIO_DEFAULT_STRING)
avatar    = models.ImageField(upload_to='members/avatars/')

I got it working in PhpStorm, can't find it in PyCharm.

2

There are 2 answers

1
bonusrk On

It is really strange, but PyCharm 2016 has this stuff, and i was to search the way to turn it OFF. I found this question, while searching "How to turn align by value".

File - Settings - Editor - Code Style - Python

There you need Other tab, where you'll find Dict Aligment settings. There is 3 types:

  • Do not align
  • Align on value
  • Align on colon

Here is a screenshot

Have no idea why they added that stuff, maybe because of platform unification, but it is exist and was turned to 'Align on value' by default for me after i've updeted PyCharm to 2016.2.

UPDATE:

A litle bit more explanations:

It was asked how to make aligns in PyCharm code formating, for example in dicts.:

Here how does dict should looks like via PEP:

i_am_a_dict = {
    key:value,
    another_key:another_value,
    a_very_long_key:one_more_value
}

That's how does it looks for me in PyCharm after platform update (and such format DO NOT match PEP, but it can be done by PyCharm settings:

 i_am_a_dict = {
        key:            value,
        another_key:    another_value,
        a_very_long_key:one_more_value
    }

It was aligned by values. And the question was about "How to align key:values in PyCharm"

0
Viaches On

You need a function from plugin String Manipulation:

MenuCodeAlign to Columns


Configure hot-keys

  • SettingsKeymap
  • Search "Align to Columns" and select
  • Add Keyboard Shortcut(Enter)
  • Set your shortcuts (my Ctrl+K)
  • Press Apply and Ok