2

Given the following file:

Stand 
a 
b 
c 

d
e
f

Stand 
a 
b 
c 

I would like to remove the lines that match the following pattern Stand\(.*\n\)\{4}

To achieve that I executed the command :g/Stand\(.*\n\)\{4}/d unfortunately just the first line of that matched lines are deleted. The result looks like the following:

a
b
c

d
e
f

a
b
c

Any clue?

1
  • 2
    :%s/Stand\(.*\n\)\{4}//g works, or :g/Stand\(.*\n\)\{4}/d+4 – Your question basically boils down to "can you delete newlines in the :g/../d pattern? I'm not entirely sure if that's possible or not, but personally I'd just use the :%s/..// for things like this. Jul 10, 2023 at 20:24

2 Answers 2

1

My understanding is that the g command is a loop over the lines of the buffer.

For every line that match the regular expression the command is executed.

:g/Stand\(.*\n\)\{4}/d

The regular expression match only the two lines that starts with Stand and so only these two lines are deleted.

2

As others have said, g command acts on the line only, not the entire matching block. For the pattern in question, you can do below.

:g/Stand\(.*\n\)\{4}/norm 4dd
2
  • 1
    Good answer. May I add that :help :global states that "for a multi-line pattern, only the start of the match matters". I'd also simplify the command by getting rid of (at least one of) the 4's. It could be something as simple as :g/^Stand/norm d} but that largely depends on the contents of the actual file.
    – Friedrich
    Jul 11, 2023 at 6:57
  • 1
    :g/…/,+4d (or maybe +3?)
    – D. Ben Knoble
    Jul 11, 2023 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.