Quantcast

Delete including newlines

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Delete including newlines

Paul-425
I have some text like this:

# Something important
# Start of the range
# anything could be here
# anything could be here
# Something important

I want to delete a range like this:

:g/^# Start of the range\n#.*\n#.*/norm 3dd

That is: the start of the range, followed by the next two lines but only if they're also comments (I'm going to automate it, so I must make sure that those lines are such).

:g/^# Start of the range\n#.*\n#.*/d

doesn't work, because it deletes only the first line. Is my 'norm 3dd' method the best way?

--

.

--
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Delete including newlines

Tim Chase
On 04/19/12 10:57, Paul wrote:
> I have some text like this:
>
> # Something important
> # Start of the range
> # anything could be here
> # anything could be here
> # Something important

> That is: the start of the range, followed by the next two lines but only if they're also comments (I'm going to automate it, so I must make sure that those lines are such).
>
> :g/^# Start of the range\n#.*\n#.*/d
>
> doesn't work, because it deletes only the first line. Is my 'norm 3dd' method the best way?

I'd usually use

  :g/^# Start of the range\n#.*\n#/.,+2d

which does as you describe.  Alternatively, if you can identify
the 2nd "Something important", you can do things like

  :g/^# Start of the range/.,/^# Something important/-1d

which will delete from "Start of the range" through the line
before ("-1") the next line matching "^# Something important".

All sorts of nifty Ex tricks here.

-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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Delete including newlines

Ben Fritz
In reply to this post by Paul-425
On Thursday, April 19, 2012 10:57:38 AM UTC-5, Jan wrote:

> I have some text like this:
>
> # Something important
> # Start of the range
> # anything could be here
> # anything could be here
> # Something important
>
> I want to delete a range like this:
>
> :g/^# Start of the range\n#.*\n#.*/norm 3dd
>
> That is: the start of the range, followed by the next two lines but only if they're also comments (I'm going to automate it, so I must make sure that those lines are such).
>
> :g/^# Start of the range\n#.*\n#.*/d
>
> doesn't work, because it deletes only the first line. Is my 'norm 3dd' method the best way?
>

The problem is that the command ran by your :g command defaults to acting on the line where the entire match starts. So by specifying a multi-line match, the match starts on the first line, and the d command will only act on that line.

You need to specify a range for your d command, as Tim suggests.

--
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Delete including newlines

Paul-425
In reply to this post by Tim Chase
On Thursday, 19 April, 2012 at 19:13:37 BST, Tim Chase wrote:
> Alternatively, if you can identify the 2nd "Something important", you can do
> things like
>
> :g/^# Start of the range/.,/^# Something important/-1d
>
> which will delete from "Start of the range" through the line before ("-1")
> the next line matching "^# Something important".

I need to make sure that the lines between are all comments, so I can't do such a range.

>I'd usually use
>
> :g/^# Start of the range\n#.*\n#/.,+2d
>
>which does as you describe.

That's a little shorter than using norm, I'll use it. Thanks.

--

.

--
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Delete including newlines

Tim Chase
On 04/19/12 17:54, Paul wrote:
> On Thursday, 19 April, 2012 at 19:13:37 BST, Tim Chase wrote:
>> Alternatively, if you can identify the 2nd "Something important", you can do
>> things like
>>
>> :g/^# Start of the range/.,/^# Something important/-1d
>>
>> which will delete from "Start of the range" through the line before ("-1")
>> the next line matching "^# Something important".

if you want to ensure that only comment-lines are found, you can
use a branching regexp conjunction:

   /^\(# Something important\|[^#]\)/

since you back off by one line either way (either you've found a
part of the comments that is important, or you've found a line
that isn't a comment).

>> :g/^# Start of the range\n#.*\n#/.,+2d
>>
>> which does as you describe.
>
> That's a little shorter than using norm, I'll use it. Thanks.

If shorter is what you want, the "." is optional/default, so you
can do

  :g/regex/,+2d

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