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.
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!
Tabularize /,\zs
See :help \zs
Since the previous pattern doesn't work, try with this:
Tabularize /[^,]\+,
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With