A S
A S

Reputation: 1235

Vim: Aligning text to columns

Let's say I have the following text:

key:value1 key:value234 key:val0
key:val1 key:value1 key:value5
key:value65799 key:value356 key:3790

Is it possible somehow to transform it to some kind of aligned columns?

key:value1          key:value234            key:val0
key:val1            key:value1              key:value5
key:value65799      key:value356            key:3790

Of course, I can replace separating spaces with tabs but that alone doesn't help...

Upvotes: 2

Views: 2315

Answers (4)

Alan Gómez
Alan Gómez

Reputation: 378

A regex based solution is as follow:

:%s/\s/ /g followed by:

:%s/\([^,]\{20}\)\(\s\+\)/\1/g

Best regards.

Upvotes: 1

statox
statox

Reputation: 2886

And if you dont mind using plugins see tabular.

Here you can visually select your lines and use:

:'<,'>Tabular/ /

There's also vim-easy-align.

Upvotes: 2

romainl
romainl

Reputation: 196516

On a Unix-like system:

:%!column -t

Upvotes: 7

Validus Oculus
Validus Oculus

Reputation: 2701

I would normally first convert spaces to #, then use Align to align based on #, then remove #.

:%s/\ /#/g
:'<,'>Align#
:%s/\ /#/g

And the result is as follows.

key:value1      key:value234  key:val0
key:val1        key:value1    key:value5
key:value65799  key:value356  key:3790

Upvotes: 3

Related Questions