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.


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

  • :%normal J was very quick and easy (see 2nd top solution below)... compared to this macro solution running it on over 50,000 lines – ihightower Jul 14 '16 at 7:17
  • @ihightower that's why i upvoted that answer as well. 6 years ago :) – akira Jul 15 '16 at 10:56
  • @akira your macro just saved my day as i needed just this macro solution today for a different purpose. – ihightower Feb 22 '17 at 15:54

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
  • Kevin he is looking to run two commands on every file, not just one. – JNK Jul 28 '10 at 20:16
  • 1
    the use of :g answers OP need. – Luc Hermitte Jul 28 '10 at 20:28
  • 2
    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
  • 3
    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 three lines

  • 1
    This is a VERY nice solution Miro. It's even better in that you can use this in standard vi as well, although strangely, when you use the trailing number in SVR4.0 vi (as on Solaris) instead of j3 making 3 columns it makes 4. (so you need to use j2 there for grouping every 3 lines) – JohnGH Nov 28 '16 at 10:52

We can also play with:

'<,'>g//s/.*\zs\n\ze.*/ /

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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