Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim substitute: Delete All between specified words

Tags:

regex

vim

vi

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.

like image 773
Conner Avatar asked Feb 20 '26 23:02

Conner


1 Answers

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:

  • :help :v
  • :help :d

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

:%s/^HTMName=\(\_.\{-}\)HTMFile=\|.*/\1/
like image 178
ryuichiro Avatar answered Feb 24 '26 00:02

ryuichiro