我想知道在 vim中是否有某种方法可以删除第一行/最后一行匹配特定模式的所有代码块.例如,我有一个带有很多#if #endif块的c ++代码我想要摆脱:
#if GGSDEBUG ---|
somecode Block to delete
#endif ---|
//code
#if GGSDEBUG ---|
somecode Block to delete
#endif ---|
//code
#if GGSDEBUG ---|
somecode Block to delete
#endif ---|
Run Code Online (Sandbox Code Playgroud)
谢谢,
使用该:global
命令查找块的所有起始行,并在那里执行命令.
:g/^#if GGSDEBUG/ [...]
Run Code Online (Sandbox Code Playgroud)
当光标位于块的第一行时,您可以:delete
通过指定以描述块结尾的模式结束的范围来阻止块:
:.,/^#endif/delete
Run Code Online (Sandbox Code Playgroud)
放在一起:
:g/^#if GGSDEBUG/.,/^#endif/delete
Run Code Online (Sandbox Code Playgroud)
您可以调整模式(例如,通过追加\n\zs$
来endif
也删除以下空行).