How to join every second line in Vim?

28

7

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.

Boldewyn

Posted 2010-07-28T17:52:21.577

Reputation: 3 835

Answers

30

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

akira

Posted 2010-07-28T17:52:21.577

Reputation: 52 754

:%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 – 2016-07-14T07:17:56.687

@ihightower that's why i upvoted that answer as well. 6 years ago :) – akira – 2016-07-15T10:56:42.797

@akira your macro just saved my day as i needed just this macro solution today for a different purpose. – ihightower – 2017-02-22T15:54:37.970

Cool, thanks for remembering the macros to me! – Boldewyn – 2010-07-29T09:21:06.793

24

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 Panko

Posted 2010-07-28T17:52:21.577

Reputation: 6 339

Kevin he is looking to run two commands on every file, not just one. – JNK – 2010-07-28T20:16:28.467

1the use of :g answers OP need. – Luc Hermitte – 2010-07-28T20:28:42.097

2This will indeed join every second line. Try it! – Kevin Panko – 2010-07-29T04:04:04.183

Thanks for the global trick. In my case however, recording the macro was easier and faster. – Boldewyn – 2010-07-29T09:20:01.663

Could anyone elaborate a bit on why this works, please? :%normal J executes J on all lines of the program, but why does it visually affect only half of them? – ssice – 2013-03-26T17:17:50.163

2Just 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 – 2013-03-26T21:48:22.093

:%norm J - such a great idea, thanks! – timss – 2013-06-08T10:33:16.213

10

What about this:

:g/$/j  

or

:g/$/j!  

and group every three lines

:g/$/j3 

Miro

Posted 2010-07-28T17:52:21.577

Reputation: 171

1This 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 – 2016-11-28T10:52:00.637

0

We can also play with:

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

Luc Hermitte

Posted 2010-07-28T17:52:21.577

Reputation: 1 575

-1

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.

JNK

Posted 2010-07-28T17:52:21.577

Reputation: 7 642

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 – 2010-07-28T17:57:35.013

1http://vimdoc.sourceforge.net/htmldoc/syntax.html - Sorry it didn't help! – JNK – 2010-07-28T17:59:03.120

Actually check this link:http://www.vim.org/scripts/script.php?script_id=2136 see if that does what you are after.

– JNK – 2010-07-28T18:00:58.123