Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

up vote 11 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

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
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
2  
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

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

500(Jj) 

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

Your Answer

 
discard

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.