Sunday, December 2, 2012

Re: how can you delete every line between two phrases?

On 12/02/12 20:15, Jiaxing Wang wrote:
> In :g/how are you?/.;/:-)/d,
> Is '/how are you?/.;/:-)/' the pattern in
> :[range]g[lobal]/{pattern}/[cmd]?
> Would you mind explaining this pattern a little? I don't quite
> understand this, thanks.

Using your template of ":[range]global]/{pattern}/[cmd]" it breaks
down as

:g/how are you?/[cmd]

where [cmd] is

.;/:-)/d

which is a range from the currently matching "how are you?" line
through the next line containing ":-)". The Ex command issued over
that range is "d"elete. If you read at

:help :range

you'll see the "/" is a way of specifying an address. You can even
stack them if you want, such as

+3;/hello/?world?+2

which will start the range 3 lines after the currently matching
line, and end the range at the location found by searching forwards
to "hello", then backwards to "world", and then going forwards two
lines. It's a bit of a crazy example, but sometimes that's exactly
what you need to specify the range you want. A recent real-world
example from my own use was something like:

:g/^\s*def [^(]*[pP]rovider/+1;'}?DEBUG?s/^/#

which commented out ("s/^/#") the lines in Python code after the
function definition, through the last DEBUG in the current paragraph
(as marked by '}).

The difference between using ";" (as I do) and using "," is one of
those things I don't fully grasp as it seems to be fairly
interchangeable in other contexts. But in this use-case as a
destination for :g commands, I find that I almost always want ";".
This is tersely documented at

:help :;

I hope that helps disassemble my answer so that you can go do crazy
things with vim and impress your colleagues, too. :-)

-tim



--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: