简体   繁体   English

如何删除所有内容,直到下一个模式匹配vim中的文件结尾*或**

[英]How to delete everything until the next pattern match **or** *the end of the file* in vim

Can I get vim to delete everything until the next pattern match or the end of the file ? 我可以让vim删除所有内容,直到下一个模式匹配 文件末尾吗?

I thought about searching for a pattern or the end of the file (at the same time), still allowing wraparound (so not just this ), which would then allow me to dn to delete until the next match (which would then include the end of the file), but I couldn't figure out how to encode |EOF in the search. 我考虑过要搜索模式文件的末尾(同时),仍然允许环绕(所以不仅限于this ),这将允许我dn删除直到下一个匹配项(然后包括末尾)的文件),但我不知道如何在搜索中对|EOF进行编码。

Perhaps it's possible to add something to this one instance of the delete command (eg d/Gn , which is not afaik a thing) or something? 也许可以在此delete命令的一个实例中添加某些内容(例如d/Gn ,这不是一件容易的事)或其他内容吗? Something like enabling nowrapscan just for this one command... 仅为此一个命令启用nowrapscan一样。。。

Use Case: When editing --verbose git commit messages, where the diff is included in the commit message template, I like to add a comment to the message about a section of the diff, delete that code section, and repeat until all the code diffs are gone. 用例:在编辑--verbose git commit消息(其中diff包含在提交消息模板中)时,我想在消息中添加有关diff部分的注释,删除该代码部分,并重复直到所有代码差异消失了。 I search for ^\\(@@\\|diff\\|$\\) , which finds the beginnings of a new file ( ^diff ) or hunk ( ^@@ ) or blank line ( ^$ ), and then can use dn to delete down to the next match, which works fine until the last hunk (which may extend past the end of the screen) - if I accidentally do this there, it wraps around and deletes upward . 我搜索^\\(@@\\|diff\\|$\\) ,它找到新文件( ^diff )或大块( ^@@ )或空白行( ^$ )的开头,然后可以使用dn来删除到下一场比赛,直到最后猛男(可延伸过屏幕的结束)的正常工作-如果我不小心这样做在那里,它将绕向上删除。 Easy enough to undo and dG , but still kind of annoying. 撤消和dG很容易,但是仍然很烦人。

You can use the \\%$ pattern, that matches the end of the file. 您可以使用\\%$模式,该模式与文件结尾匹配。

So d/x\\|\\%$ to delete up to the first x or the end of the file. 因此, d/x\\|\\%$删除最多前x或文件末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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