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?

  • 1
    dunno about vim, but easy to do on bash.. split -l 4 ip.txt op ; paste op* – Sundeep Jul 22 '16 at 11:16

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*

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


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.

Your Answer

