How to join every second line in Vim?

I'd like to join a lot (~1000) of lines, but only every odd with the next one. By hand I could do

Jj

500 times and have it done. However, how can I execute these two statements 500 times in one single command? Typing

500Jj

will join the next 500 lines and then moving down one line.

Example:

I have:

a
b
c
d
e
f
g
h

I want:

a b
c d
e f
g h

Edit: I tried mapping:

:map X Jj
500X

but apparently I should read the mapping docs again. Doesn't work.


Solution 1:

i would do this:

  1. start recording a macro 'q': qqJjq

  2. replay the macro 'q' 500 times: 500@q

(actually it is not a macro called 'q', it is a named register called 'q'. instead of interactively fill that register as in 1., you could also do :let @q = "Jj" and then do 2.)

Solution 2:

To do this on every line of the file:

:%normal J

or, shorter:

:%norm J

To do this on just a portion of the file, select the lines with V or get a range some other way:

:'<,'>global/^/normal J

or, shorter:

:'<,'>g/^/norm J