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

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....


Solution 1:

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

Solution 2:

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!

Solution 3:

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.