简体   繁体   中英

Vim substitute: Delete All between specified words

A piece of the file I am trying to cut

HTMName=[...] owns the means of production. Proletariat do not and sell their labor power.
HTMFile=x
ClickPlay=0
TestElement=0

Type=HTML
Cors=123
DisplayAt=215
Hyperlink=0
HTMName=Bourgeois
HTMFile=x
ClickPlay=0

End result

[...] owns the means of production. Proletariat do not and sell their labor power.
Bourgeois

I am aware that the top and bottoms of the file will have remainders. I have tried the following

:s%/^\sHTMFile[\s\S]*\n\s*HTMName=$//g

And many other variations which all return no pattern found. My grasp of regex is pretty weak.

At first, I would delete all lines which don't contain the searched pattern ( HTMName= at the start of the line), and then I would remove the searched pattern

:v/^HTMName=/d
:%s///

:v means execute a command (in this case ":d") on the lines where {pattern} (in this case "HTMName=" at the start of the line) does NOT match.

:d means delete lines.

See more in help:

PS: If your pattern is not in just one line, you can try

:%s/^HTMName=\(\_.\{-}\)HTMFile=\|.*/\1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM