3

A lot of times, I have a list of initializers in some of my code, like this:

class Foo(object):
    def __init__(self, data):
        self.foo       = data.getFoo()
        self.bar       = data.getBar()
        self.something = data.getSomething()

As you can see, I like my code aligned like a table. In a lot of cases, the above code can be generated by scripting Vim, coming from the output of some other program (DESCRIBE "foo"; in a database for example). Unfortunately, the scripted output usually looks like this, first:

class Foo(object):
    def __init__(self, data):
        self.foo = data.getFoo()
        self.bar = data.getBar()
        self.something = data.getSomething()

So after the automatic generation of th assignment statements, I'll have to manually align all statements for the desired look.

Now: Is there a way to get vim to align those "second halves"of the statements automatically?

Dave Vogt
  • 18,600
  • 7
  • 42
  • 54
  • Could you explain more clearly how you obtain this output? With `:r something`? – romainl May 18 '12 at 17:37
  • I found a vim script that purports to do this, but I don't want to post it as an answer because I haven't actually used it: http://www.vim.org/scripts/script.php?script_id=294 . See also this SO question: http://stackoverflow.com/questions/8964953/align-text-on-an-equal-sign-with-vim – asfallows May 18 '12 at 18:37
  • @romainl - The source could be anything from pasted/processed SQL to a file listing to whatever.. might be read by doing some `:r! ... ` or anything, really. – Dave Vogt May 22 '12 at 16:47

3 Answers3

3

The tabular plugin does exactly this. You can see it in action (and learn how to use it) here.

UPDATE: I'll give a brief explanation about the plugin usage, but no explanation will be better then Drew's video, so I strongly suggest everybody to watch it.

To use the plugin just call :Tab /= and it will align all the equal signs in the file. If you want to specify which line you want to align just give it a range :5,10Tab /= or use the visual mode (v or V) to select the desired lines, press : and insert the Tabularize command, your command line will look like this: :'<,'>Tab /=.

The argument in the Tabcommand is a Regular Expression, this means you can use this command to align many things. You'll be restricted only by your Regular Expression knowledge.

Sorry for any English mistake :D

Magnun Leno
  • 2,728
  • 20
  • 29
  • Perfect, I hope I will rememver when I need it ... To make the answer even better, you could explain how to use it in the example (like `:Tab /=`). – mliebelt May 18 '12 at 18:58
  • 1
    @mliebelt I've added a quick explanation. But as I said, no explanation I gave will be better then Drew's video. – Magnun Leno May 21 '12 at 12:46
1

An alternative to the already mentioned Tabular plugin is the venerable Align plugin.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
0

One naive approach would be to first make enough space around the equal signs:

:s/=/                         =/

Then, block-selecting (Ctrl-V) so that all the = characters and everything that follows is selected. Yank(y) that, paste it somewhere else.

Next, un-indent the pasted lines (10< is usually sufficient) until they're aligned to the leftmost position. Then, block-select again and paste to where they were cut off.

This feels like a lot of work though, for the desired effect.

Dave Vogt
  • 18,600
  • 7
  • 42
  • 54
  • indeed a lot of work. why not a simple :s that just adds enough tabs/spaces to all lines except the line with the longest identifier before the = – pb2q May 18 '12 at 23:14