Vim 到 select 一段文本行直到特定字符的最快方法

Vim fastest way to select a block of text lines until a specific character

我有一个包含一些维基代码 (tikiwiki) 的页面,其中包括 html 这样的 :

{DIV(class="Act PersonInterested")}
{HTML()}
  multiple 
  lines 
  of 
  html (svg) 
  code
{HTML}
{DIV}

{DIV(class="Act CHActivePFOnly")}
{HTML()}
  multiple 
  lines 
  of 
  html (svg) 
  code
{HTML}
{DIV}

我想用 vim 删除 {HTML()}{HTML} 维基标签之间的文本。 dt{ 不起作用...

我找到的最方便的选择是录制一个宏,比如那个:

qa   -> Records macro in register a
v/{  -> Visual selects text until next '{' symbol
q    -> Stops recording
@a   -> Applies macro from the current line

然后我移至下一行并使用“@a”重新应用宏,依此类推,直到清空所有标签。

我的问题是,一定有一种更快的方法,但我忽略了...比如 g: one 甚至更简单。我很高兴了解它,而且,我在这里没有找到太多答案,也没有找到关于那个特定问题的回避。

谢谢!

你可以这样做:

:g/{HTML(/norm jd/HTML^M

或:

:g/{HTML(/norm jV/HTML^Mkd

如果你对视觉模式更有信心

Ctrl+V,然后按Enter得到^M .

我没看错问题吗?

%s/{HTML()}\zs\_.\{-}\ze{HTML}//g

以上命令对你有用吗?

您也可以只录制更复杂的宏,然后重复它。

qa           -> Records macro in register a  
/{html()<cr> -> Find next   
j            -> Go down one line   
d/{html}<cr> -> delete to closing html tag  
q            -> Stops recording  
100@a        -> Applies macro 100 times or less if there is no matches  

以下就够了

g/{HTML/,/{HTML/ d_

与其他答案一样,这假设您的真实标签之间没有任何 {HTML... 。如果这样做,唯一可靠的方法是使用解析器。

细分

g              : start a global command
/              : separator
{HTML          : search for {HTML
/              : separator
,/{HTML/       : set a range from the previous search result up until the next {HTML tag
d_             : delete to the empty register. As I have my clipboard synchronized with deletes, without the `_`, each delete goes to my windows clipboard slowing things down a lot in large files.