Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
1  
dunno about vim, but easy to do on bash.. split -l 4 ip.txt op ; paste op* – sp asic 6 hours ago
up vote 2 down vote accepted

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*
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.