Gordon Yuan Gao
Gordon Yuan Gao

Reputation: 694

How to tabularize with delimiter of groups of spaces in Vim

I have some lines like this

abc  defg  hijklmnopq
abc    defghi  jklmn opq
abc  defgh  ijklmno pq

I want them to be tabularized into at most three columns with at least 2 spaces in between columns, i.e.

abc  defg    hijklmnopq
abc  defghi  jklmn opq
abc  defgh   ijklmno pq

What Tabularize command should I use in Vim?

Upvotes: 1

Views: 1097

Answers (1)

glts
glts

Reputation: 22684

Given your input and output samples, the following command should do it.

:Tab /\s\+\zs\s/l1c0

You can abbreviate :Tabularize to :Tab to save a few keystrokes. The regular expression relies on a property of your data, namely that there is more than one space character separating the columns; otherwise it would be impossible to know which text belongs to which column.

We choose the final space as the delimiter character and then align the text left with a padding of 1. The \s serves as the second padding space.

In the future, and as always, please consider the :help. :help tabular has extensive and very readable documentation that describes in detail all of the above.

Upvotes: 4

Related Questions