Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

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


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


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


I have:


I want:

a b
c d
e f
g h

Edit: I tried mapping:

:map X Jj

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

share|improve this question
up vote 18 down vote accepted

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.)

share|improve this answer
Cool, thanks for remembering the macros to me! – Boldewyn Jul 29 '10 at 9:21
Nice answer, thanks. Just love vi(m). – Patrick Mar 11 '15 at 20:19

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
share|improve this answer
Kevin he is looking to run two commands on every file, not just one. – JNK Jul 28 '10 at 20:16
the use of :g answers OP need. – Luc Hermitte Jul 28 '10 at 20:28
This will indeed join every second line. Try it! – Kevin Panko Jul 29 '10 at 4:04
Thanks for the global trick. In my case however, recording the macro was easier and faster. – Boldewyn Jul 29 '10 at 9:20
Just a guess -- it executes the command on each line in order, and after doing the first line, the second line is now gone (having been joined with the first line), so it is forced to move on to the third line. – Kevin Panko Mar 26 '13 at 21:48

What about this

and group every 3 lines

share|improve this answer

We can also play with:

'<,'>g//s/.*\zs\n\ze.*/ /
share|improve this answer

I'm not a user of Vim, but checking the online docs it looks like


might work since it parses things insides parentheses as a unit.

share|improve this answer
Hm, thanks for the try, but in my version Vim just ignores the parentheses. Could you tell me the link where you found this syntax? – Boldewyn Jul 28 '10 at 17:57
1 - Sorry it didn't help! – JNK Jul 28 '10 at 17:59
Actually check this see if that does what you are after. – JNK Jul 28 '10 at 18:00

Your Answer


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.