vimedit

Delete everything except multiline pattern, in which the end is defined as 1 line after the first line


I wish to delete everything that does not match a pattern

What I have tried:

I do slash-forward-search on this regex pattern:

\d\+_.*\n.*

which matches two pairs of lines:

A
50
Y
Not allowed
According to registry coding
3_Tumour_ID                      ### match
Tumour identification            ### match
A
50
Y
Not allowed
According to registry coding
4_Day_DoB                        ### match
Day of birth                     ### match
F
2

Then i try to delete everything but these matches using this:

:v//d 

I get this:

3_Tumour_ID
4_Day_DoB

But I was expecting this:

3_Tumour_ID
Tumour identification
4_Day_DoB
Day of birth

Which is correct because it keeps all the matched lines, not just the first line of each match.

The second line I want to match (in each pair of lines I wish to match) is matched solely on the premise that it immediately succeeds the first line in each pair.


Solution

  • you cannot use :v//del comd to do it. Because your search pattern contains new line, however :g, :v will do line based processing. Therefore the Tumour... and Day of... lines will be processed by :v again, finally removed.

    What you can try is:

    qaq    (to clear "a register)
    :g//norm! "A2Y   (this copy all matched lines into register a)
    ggVG"ap
    

    Note: I used 2Y because you have one \n in your pattern, that is, your matched result will always be two lines.