12

I would like to automatically align lines of python variable assignments in vim.

For example I would like to change this:

a = 1
banana = 2

into this

a      = 1
banana = 2

automatically in vim.

Is there a way to do this?

7
  • 6
    I'd like to mention that PEP 8 (python.org/dev/peps/pep-0008) explicitly discourages this kind of formatting. Dec 10, 2008 at 22:34
  • 3
    More importantly, is there a reason for doing this? What's all the extra whitespace for?
    – S.Lott
    Dec 10, 2008 at 22:40
  • 3
    saua, noted thanks! S.Lott, it's mostly because it is easier for me to read. Dec 10, 2008 at 22:41
  • 4
    I agree with Mr. Eden here. I even indent "from x import y"'s! It reads better and looks neater, IMHO.
    – Walter
    Dec 11, 2008 at 19:58
  • 1
    @PaulD.Eden I also noticed the style guide discouraging this. What is your take on it now, years later? Apr 18, 2013 at 5:38

4 Answers 4

15

You are probably looking for the Align plugin for vim:

http://www.vim.org/scripts/script.php?script_id=294

Once installed, you simply select the text you want to align (Shift V) and type:

:Align =

This will align the text based on the = character, so you can use anything you want really.

1
  • ⁺¹ for the mention that it is a plugin. I am found the command in a vim wiki, and was wondering why VIM concerns this command wrong. Now I decided to google more, and found here that this is a plugin. Btw, Emacs has the functional out of the box :Ь
    – Hi-Angel
    Oct 22, 2014 at 16:01
1

The Align plugin does this and much more.

1

Try the align plugin.

It's described here...

0

this will do the work :

vap

:!column -t

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.