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.

link|improve this question

feedback

4 Answers

up vote 6 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.)

link|improve this answer
Cool, thanks for remembering the macros to me! – Boldewyn Jul 29 '10 at 9:21
feedback

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
link|improve this answer
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
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
feedback

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.

link|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  
vimdoc.sourceforge.net/htmldoc/syntax.html - Sorry it didn't help! – JNK Jul 28 '10 at 17:59
Actually check this link:vim.org/scripts/script.php?script_id=2136 see if that does what you are after. – JNK Jul 28 '10 at 18:00
feedback

We can also play with:

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

Your Answer

 
or
required, but never shown

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