41

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.

5 Answers 5

41

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

4
  • 1
    :%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, 2016 at 7:17
  • @ihightower that's why i upvoted that answer as well. 6 years ago :)
    – akira
    Jul 15, 2016 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, 2017 at 15:54
  • Ideally a macro wouldn't be needed for something this simple, but the other answers (at least the ones that looked nice to me) didn't work for me unfortunately. This one didn't work for me either, due to there being a blank line between each set of lines that had text, but at least this solution allowed me to make the necessary modifications to get the job done for me. Inelegant but got the job done. Feb 21, 2023 at 0:23
38

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
8
  • 1
    the use of :g answers OP need. Jul 28, 2010 at 20:28
  • 2
    This will indeed join every second line. Try it! Jul 29, 2010 at 4:04
  • 1
    Thanks for the global trick. In my case however, recording the macro was easier and faster.
    – Boldewyn
    Jul 29, 2010 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. Mar 26, 2013 at 21:48
  • 1
    For joining 3 rows, delighted to see that :%norm JJJ will work just as well
    – Ari
    Feb 19, 2021 at 1:22
26

What about this:

:g/$/j  

or

:g/$/j!  

and group every three lines

:g/$/j3 
3
  • 2
    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, 2016 at 10:52
  • this is great! way quicker and cleaner than the actual accepted answer that record and redo the command n times!
    – Simon C.
    Dec 7, 2022 at 17:13
  • I accidentally upvoted this, but it doesn't work for me, and there's no explanation of the difference between j and j! here. Feb 21, 2023 at 0:07
-1

We can also play with:

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

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

500(Jj) 

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

3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .