# Transforming a long table into multiple columns

Suppose I have the following table:

``````E   12.02
T   9.10
A   8.12
O   7.68
I   7.31
N   6.95
S   6.28
R   6.02
H   5.92
D   4.32
L   3.98
U   2.88
C   2.71
M   2.61
F   2.30
Y   2.11
W   2.09
G   2.03
P   1.82
B   1.49
V   1.11
K   0.69
X   0.17
Q   0.11
J   0.10
Z   0.07
``````

Values are separated by tabs.

I want to put this table in n columns of maximum length m.

For example, for n=7 (m=4):

``````E   12.02   I   7.31    H   5.92    C   2.71    W   2.09    V   1.11    J   0.10
T   9.10    N   6.95    D   4.32    M   2.61    G   2.03    K   0.69    Z   0.07
A   8.12    S   6.28    L   3.98    F   2.30    P   1.82    X   0.17
O   7.68    R   6.02    U   2.88    Y   2.11    B   1.49    Q   0.11
``````

How would I do that?

• dunno about vim, but easy to do on bash.. `split -l 4 ip.txt op ; paste op*` Commented Jul 22, 2016 at 11:16
• There is an old vim plugin by Charles Campbell that supposedly does exactly what you want. Commented Oct 5, 2019 at 10:27

Using the *nix commands `split` and `paste`

``````\$ split -l 4 ip.txt op ; paste op* > ip.txt
``````

Or, from within vim as suggested by OP:

``````:!split -l 4 % /tmp/split; paste /tmp/split* > %; rm /tmp/split*
``````
• Wow, learned a useful tip. thanks. BTW if the column lengths are different (with max value), can we paste the split files so that each column is aligned? (specifying column start space) I checked the man paste but couldn't find the answer. Commented Feb 19, 2021 at 7:40

If you want to do that in vim you can use the following macro:

``````qa^V3j\$d"_4dd^V3jI<tab><Esc>Pq
``````

Which can be decomposed like this:

``````qa          Record the macro in the register a
^V          Enter visual block mode (use ctrl-v)
3j\$         Select the lines you want to put in a column
d           Delete them
"_4dd       Delete the lines which contained the column in the black hole register
^V          Enter visual block mode again
3j          Select the lines
I           Start inserting characters in front of the selection
<tab>       Insert a tab character
<esc>       Exit insert mode
P           Insert the column you previously deleted
q           Stop recording the macro
``````

You can then use @a as long as you still have lines to put in the columns.

Also I would recommend you to use a tool like `awk` or `split` like @sp asic suggested.