tags:

views:

43

answers:

3

I have a text which is made of lines and some of them look like that:

A test1
test test
test test

A test2
test test
test test

The line starts with A (arbitrary but unique string) and ends with an empty line. I like to remove all redundant newline symbols from the real lines (without affecting other lines, not matching the /^A/) and make the lines look like that:

A test1 test test test test

A test2 test test test test

Once I'm at /^A/, I can use the command :.,/^$/-2s/\n/ / (range .,/^$/-2 applied to :s///). Going over the whole file and doing it manually is rather mundane thus I have attempted to combine that with :g/^A/:

:g/^A/.,/^$/-2s/\n/ /

But that produced error since in command after :g//, the range wasn't recognized. I have tried to use normal too - :g/^A/normal .,/^$/-2s/\n/ / and :g/^A/exe 'normal .,/^$/-2s/\n/ /' - with no luck.

Is there any way to run from under :g// a command with range? Or is there any other easy (== without scripting) way to accomplish the same formatting task?

+2  A: 

Try this. To enter the ^M, use ctrl-v enter.

:g/^A/normal V/^$/^MkJ
ar
It works. Can you please also explain the magic? It appears that after `normal` goes something similar to a macro.
Dummy00001
OK. Found it myself under *surprise* `:h :normal`: `This makes it possible to execute Normal mode commands typed on the command-line. {commands} is executed like it is typed.`
Dummy00001
A: 

Maybe that : :g/^A/normal JJ

jageay
This only deals with three line blocks.
ar
A: 

combining :g with commands that take a range works for me. e.g.

:g/^A/.,/^$/-1join 

as does your original version:

:g/^A/.,/^$/-2s/\n/ /

N.B. If you want brevity then the join command can be abbreviated to j, so the first version becomes:

:g/^A/.,/^$/-1j 
Dave Kirby