Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge vertical lists in Vim

Tags:

vim

list

Is there a way which allows me to merge lists vertically?

For example, if I have have these two lists:

A E
B F
C G
D H

I would like to end up with the following:

A
E
B
F
C
G
D
H
like image 508
David Clark Avatar asked Sep 04 '17 08:09

David Clark


3 Answers

This is simple, just place the cursor on the column between the lists. Insert visualblock-mode <C-v>, mark the whole column, hit r to replace it, and then <CR> and you have what you want.

like image 84
Doktor OSwaldo Avatar answered Nov 16 '22 09:11

Doktor OSwaldo


:%s/\v^(\w) /\1\r/g

: ........... command
% ........... whole file
\v .......... very magic (avoid backslashes)
(\w) ........ word character
\1 .......... all patter in parenthesis 
\r .......... Carriage Return "Enter"
g ........... globally
like image 24
SergioAraujo Avatar answered Nov 16 '22 11:11

SergioAraujo


You could also do it with an external filter. Mark the relevant lines in visual mode and press !. The following filter does what you want on a POSIX system:

paste -sd' ' | tr ' ' '\n'
like image 1
Thor Avatar answered Nov 16 '22 10:11

Thor