开发者

vim: combine :g with command which uses range

开发者 https://www.devze.com 2023-01-12 10:06 出处:网络
I have a text which is made of lines and some of them look like that: A test1 test test test test A test2

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 at开发者_如何学Ctempted 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?


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

:g/^A/normal V/^$/^MkJ


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 


Try this maybe:

:g/^A/normal JJ
0

精彩评论

暂无评论...
验证码 换一张
取 消