Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I align like this with VIM's Tabular Plugin?

This is my code:

john,betty,wally,beth 
walter,george,thomas,john
herbert,bob,petty,mick`

Does anyone know how to align it to this with VIM's Tabular Plugin:

john,    betty,  wally,  beth
walter,  george, thomas, john
herbert, bob,    petty,  mick

I know how to do this in Align Plugin but can't find out how it works in Tabular.

like image 845
Reman Avatar asked Mar 25 '11 18:03

Reman


4 Answers

I'm not a master in this, and everything I'll say here is based on my understanding of Tabularize.

Tabularize always splits things in fields, described by the regex. For example:

abc,d,e
a,b,cde

:Tab /,

Causes it to divide each line like the following:

|abc|,|d|,|e|
|a|,|b|,|cde|

Then each field is padded with spaces so the delimiter field align — and everything receives and extra-space by default (except for the last field, I think).

|abc |, |d |, |e  |
|a   |, |b |, |cde|

Resulting in:

abc , d , e
a   , b , cde

By adding flags, you can control alignment and padding for each field. If you provide less flags than needed, they are reused. So, to make everything align the same way, say left with padding 0 you can use a single flag that will be repeated for every field. So:

abc,d,e
a,b,cde

Tab /,/l0 <-- or c0, or r0 or whatever

abc,d,e
a  ,b,cde

The conclusion I have I my mind is that there isn't much sense in having a zero width field (like :Tab /,\zs), probably causing it to get the first character after the pattern and messing up, cutting it.

Now, for me :Tab /[^,]\+, didn't worked properly, generating doubled spaces:

john,     betty,   wally,  beth
walter,   george,  thomas, john
herbert,  bob,     petty,  mick`
        ^^       ^^

I think that's because there is no intermediate field. This pattern, makes a field delimiter reside side by side with another one, like this:

|john,||betty,||wally,||beth|

Then the zero with field generated (between delimiters) is also padded with the extra 1 space by default.

|john, | |betty, | |wally, ||beth| <-- for some outrageous reason the las one is cut.

john,  betty,  wally, beth

How to solve it?

I'd open space for a delimiter, that doesn't cause a delimiter to be near others. How? Simply adding a space after the comma.

john,betty,wally,beth 
walter,george,thomas,john
herbert,bob,petty,mick`

:%s/,/, /g

john, betty, wally, beth 
walter, george, thomas, john
herbert, bob, petty, mick`

Now you can align everything on spaces and zero padding:

john, betty, wally, beth 
walter, george, thomas, john
herbert, bob, petty, mick`

:Tab / /l0

john,    betty,  wally,  beth
walter,  george, thomas, john
herbert, bob,    petty,  mick`

I hope that helps understanding Tabularize!

like image 192
sidyll Avatar answered Nov 04 '22 13:11

sidyll


Tabularize /,\zs

See :help \zs

Since the previous pattern doesn't work, try with this:

Tabularize /[^,]\+,
like image 34
Raimondi Avatar answered Nov 04 '22 15:11

Raimondi


Building on Raimondi's answer,

:Tabularize /,\zs/l0r1

makes more sense to me and it's also in just one command :-).

The l0r1 means left-align with 0 space in the left padding and 1 space in the right padding.

like image 21
hobbes3 Avatar answered Nov 04 '22 13:11

hobbes3


I have something similar I'm doing. I regularly need to turn comma-delimited values into columns:

a, b
a,b,c
a, b, c
a, b, c
ab,cd,ef
ab, cd,ef
ab,cd
ab, cd

john, betty davis, wally, beth 
walter, george, thomas, john
herbert, bob, petty, mick

Using :s/,\s*/, /g preps the values to add a single space after the commas:

a, b
a, b, c
a, b, c
a, b, c
ab, cd, ef
ab, cd, ef
ab, cd
ab, cd

john, betty davis, wally, beth 
walter, george, thomas, john
herbert, bob, petty, mick

Running :Tab /,\zs \+/10 turns them into:

a,  b
a,  b,  c
a,  b,  c
a,  b,  c
ab, cd, ef
ab, cd, ef
ab, cd
ab, cd

john,    betty davis, wally,  beth
walter,  george,      thomas, john
herbert, bob,         petty,  mick

Now I just need to figure out how to turn them into a vmap that works.

like image 1
the Tin Man Avatar answered Nov 04 '22 13:11

the Tin Man