Vim substitute: Delete All between specified words

时间:2016-12-02 05:19:04

标签: 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.

1 个答案:

答案 0 :(得分:3)

首先,我会删除所有不包含搜索模式的行(行的开头为int main(void) { char name[21]; char command; while (1) { scanf("%c", &command); clnInpBuf(); switch (command) { case 'a': case 'r': scanf("%20s", name); clnInpBuf(); printf("Command was:\n%c %s\n", command, name); // if ('a'==command) addRecord(name); else removeRecord(name); break; case 'p': // TODO: add output break; case 'e': return 0; break; // this is not realy needed default: printf("Wrong command\n"); } } } ),然后删除搜索的模式

HTMName=

:v/^HTMName=/d :%s/// 表示在{pattern}(在这种情况下,行的开头“HTMName =”)不匹配的行上执行命令(在本例中为“:d”)。

:v表示删除行。

在帮助中查看更多内容:

PS:如果您的模式不在一行中,您可以尝试

:d