TransWikia.com

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

Super User Asked by kine on November 9, 2021

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

6 Answers

Ideally you would like to use a command like

column -t -o 't' file

But this doesn't work, since the column program does not escape the t to interpret it like a Tab. But you can still insert a literal tab. To do so, press Ctrl-V and then the Tab key. So your command should be something like this:

column -t -o '        ' file

Where the big ' ' is in fact a tab.

This works pretty neat. You can also run it inside vim. Visually select the lines you want to columnate, and then run:

:'<,'>!column -t -o '^I'

The '<,'> will automatically be written by vim, and the '^I is how vim will represent the literal tab, which you can insert with the method explained above.

Answered by Hobber on November 9, 2021

Using godlygeek/tabular plugin you can select

:%Tabularize /t.*/

As mentioned by @paul-rougieux in the comments we can abreviate the above command to:

:%Tab /t.*/

Answered by SergioAraujo on November 9, 2021

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!

Answered by Cheezmeister on November 9, 2021

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

Answered by Ingo Karkat on November 9, 2021

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.

Answered by romainl on November 9, 2021

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.

Answered by Ingo Karkat on November 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP