Vim从字符到行尾删除

app*_*454 0 vim

我试图想出一些东西,它会从给定角色开始删除所有文本到行尾.

例如,在下面的示例中,我想只保留IP地址:

192.168.2.121/32 -m comment --comment "blah blah bye bye"  -j DROP
10.1.3.207 -m comment --comment "much longer comment with all kinds of stuff" -j DROP
172.16.1.0/24 -m comment --comment "Drop this range" -j DROP
Run Code Online (Sandbox Code Playgroud)

要删除的模式是-m,即从左侧读取,遇到第一个" - ".从" - "到行尾应该删除文件中的每一行.

我对这一个感到困惑,指导将不胜感激.

Lie*_*ers 8

全球指挥将是一个很好的选择

:g/-/norm nD
Run Code Online (Sandbox Code Playgroud)

说明

:g         : Start a Global Command (:h :g for extra help on global commands)
/-         : Search for -
/norm nD   : Execute nD in Normal Mode where 
               n - jumps to the match
               D - delete to the end of the line
Run Code Online (Sandbox Code Playgroud)

  • @anishjp - 是的,尝试 `%norm 0dt<character>` (2认同)

CWL*_*Liu 8

在普通模式下有一个简单的方法可以做到这一点:

  1. /-m 让光标移动到文件中第一个出现的“-m”处。
  2. d$删除从光标到行尾的字符。
  3. n找到另一个“-m”。
  4. .重做第 2 步。


Ken*_*ent 7

是不是很简单:

:%s/-m.*//
Run Code Online (Sandbox Code Playgroud)

?

或者我没有理解问题对吗?


soy*_*uka 1

我会注册一个宏,例如:

  1. 将光标放在第一行的位置0
  2. ql开始在信件上注册宏l
  3. t-D+
  4. q结束宏
  5. 根据需要多次启动宏,例如:3@l启动三次

的解释t-D+

  • t-位于下一次出现的前面-
  • D删除直到结束
  • +, 跳转到字符串开头的下一行,以便我们可以链接宏(l在 vim 上也应该工作,因为你删除到最后)

正如 @Nobe4 所说,您还可以在一行上注册宏(例如qlt-Dq),然后在视觉选择上重复:VG:normal!@l