Conner
Conner

Reputation: 823

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.

Upvotes: 3

Views: 91

Answers (1)

ryuichiro
ryuichiro

Reputation: 3875

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/

Upvotes: 3

Related Questions