How to columnize text with tabs (in vim or on the shell)

6

I have a frequent need to manually manipulate tab-delimited text for data entry and other purposes. When I do this, it helps if the text is aligned properly into columns. For example (assuming 4-space tabs):

# original format
abcdefghijklmnop    field2
abcdefgh    field2
abcdefghijkl    field2

# ideal format
abcdefghijklmnop    field2
abcdefgh            field2
abcdefghijkl        field2

I am very familiar with using the column utility to columnize text this way, but the problem is that it uses spaces to align the columns, and I specifically need tabs. This requirement also appears to rule out the Tabularize plug-in.

Is there any way that I can columnize text with tabs specifically, either within vim or at the shell? It looks like I might be able to do it with groff/tbl, but honestly I'd rather columnize it by hand than mess with that....

kine

Posted 2013-10-29T19:59:56.000

Reputation: 1 669

Answers

5

The csv.vim - A Filetype plugin for csv files also supports tab-delimited files, and has :ArrangeColumn and :UnArrangeColumn commands for that.

Ingo Karkat

Posted 2013-10-29T19:59:56.000

Reputation: 19 513

If you want to change colors, the fg colors are hardcoded in macvim.

https://github.com/b4winckler/macvim/blob/master/src/syntax.c#L7580-L7587

– Nick – 2014-09-24T02:15:44.350

some sort of documented example of the syntax to use would help, since none exist on on the github page for that plugin. – Ryan Tuck – 2018-03-30T20:25:17.090

This actually sounds really promising. I will take a look and get back to you. Thanks! – kine – 2013-10-30T17:11:24.917

I haven't actually tried this yet, but i'm going to mark it as the accepted answer based on the documentation of the plug-in, and the fact that the only other useful answer was written by you anyway. Cheers. – kine – 2013-11-03T16:34:47.337

5

Set the tab display size to just larger than your longest field.

# original format
abcdefghijklmnop<Tab>field2
abcdefgh<Tab>field2
abcdefghijkl<Tab>field2
I have spaces!<Tab>field2

In Vim,

:echo len("abcdefghijklmnop") " Reports 16
:set noexpandtab tabstop=17

You can probably condense this to one command, but I don't know how. If you're running a modern Vim, :set list will indicate hard tabs with a fancy character (which you can also configure). Otherwise they'll just show as ^I (not what you want) or whitespace.

# displays like this
abcdefghijklmnop>field2
abcdefgh>        field2
abcdefghijkl>    field2
I have spaces!>  field2

EDIT: An example from a real running vim!

Cheezmeister

Posted 2013-10-29T19:59:56.000

Reputation: 345

1

When you have properly space-aligned the table (with the mentioned Tabularize or the alternative Align plugin), you can then convert the spaces to tabs with the following commands:

:%retab!
:%substitute/ \+/\t/g

The first command replaces indent with tabs where possible without changing the widths (this assumes you've :set noexpandtab), the second then transforms the left-over spaces to (larger) tabstops.

Ingo Karkat

Posted 2013-10-29T19:59:56.000

Reputation: 19 513

I guess this will work, but only if i can be absolutely certain that there are no spaces within the fields themselves. Otherwise, i am not sure how it would be able to tell the difference between spaces within a field and the columnating ones left over by :retab. I will accept this answer if nobody has a more ideal solution. – kine – 2013-10-30T02:58:11.637

The initial alignment wouldn't handle spaces within the fields, neither (but align on them, too). If you have such data, you'd probably have to substitute those inner spaces temporarily. – Ingo Karkat – 2013-10-30T06:15:56.387

0

Using godlygeek/tabular plugin you can select

:%Tabularize /\t.*/

SergioAraujo

Posted 2013-10-29T19:59:56.000

Reputation: 211

-1

Tab-separated columns only work when the program used to display them is able to expand/shrink tab characters to honor their actual meaning. Vim, and I believe most other plain text editors, is unable to use tabs for tabulation because tabs have a single fixed width. Vim can use a mix of tabs and spaces to obtain a roughly similar result but what you'd get is obviously not tab-separated anymore.

You'd need to convert all those tab/spaces mixes into single tabs on each write.

In short, a plain text editor is probably not the right tool for the job.

romainl

Posted 2013-10-29T19:59:56.000

Reputation: 19 227

"tabs have a single fixed width"

Not true. See my answer. Many other editors have configurable tab size as wel. – Cheezmeister – 2014-06-01T21:50:39.283