简体   繁体   English

Vim:删除包含模式的所有行的字符

[英]Vim: delete until character for all lines containing a pattern

I'm learning the power of g and want to delete all lines containing an expression, to the end of the sentence (marked by a period). 我正在学习g强大功能,并希望删除包含表达式的所有行,到句子的末尾(用句点标记)。 Like so: 像这样:

There was a little sheep. The sheep was black. There was another sheep.

(Run command to find all sentences like There was and delete to the next period). (运行命令查找所有句子, There was并删除到下一个时期)。

The sheep was black.

I've tried: 我试过了:

  1. :g/There was/d\\/\\. in an attempt to "delete forward until the next period" but I get a trailing characters error. 试图“向前删除直到下一个时期”,但我得到一个trailing characters错误。
  2. :g/There was/df. but get a df. is not an editor command error. 但得到一个df. is not an editor command error. df. is not an editor command error.

Any thoughts? 有什么想法吗?

The action associated with g must be able to act on the line without needing position information from the pattern match that g implies. g相关联的动作必须能够在行上行动而不需要来自g暗示的模式匹配的位置信息。 In the command you are using, the delete forward command needs a starting position that is not being provided. 在您使用的命令中,delete forward命令需要一个未提供的起始位置。

The problem is that g only indicates a line match, not a specific character position for it's pattern match. 问题是g只表示一个行匹配,而不是它的模式匹配的特定字符位置。 I did the following and it did what I think you want: 我做了以下,它做了我认为你想要的:

:g/There was/s/There was[^.]*[.]//

This found lines that matched the pattern There was , and performed a substitution of the regular expression There was[^.]*[.] with the empty string. 此发现,相匹配的图案线There was ,和执行正则表达式的一个取代There was[^.]*[.]与空字符串。

This is equivalent to: 这相当于:

:1,$s/There was[^.]*[.]//g

I'm not sure what the g is getting you in your use case, except the automatic application to the entire file line range (same as 1,$ or % ). 我不确定g在你的用例中是什么,除了自动应用到整个文件行范围(与1,$% )。 The g in this latter example has to do with applying the substitution to all patterns on the same line, not with the range of lines affected by the substitution command. 后一个示例中的g与将替换应用于同一行上的所有模式有关,而不是与替换命令影响的行范围。

I'd just use a regex: 我只是使用正则表达式:

%s/There was\_.\{-}\.\s\?//ge

Note how \\_. 注意如何\\_. allows for cross-line sentences 允许跨行句

You can use :norm like this: 你可以使用:norm

:g/There was/norm 0weldf.

This finds lines with "There was" then executes the normal commands 0weldf. 这找到带有“有”的行然后执行正常命令0weldf. .

0 : go to beginning of line 0 :开始行
w : go to next word (in this case, "was") w :转到下一个单词(在这种情况下,“是”)
e : go the end of the word (so cursor is on the 's' of "was") e :单词的结尾(所以光标位于“是”的's')
l : move one character to the right (so we don't delete any of "was") l :向右移动一个字符(所以我们不删除任何“是”)
df. : delete until the next '.', inclusive. :删除直到下一个'。',包括在内。

If you want to keep the period use dt. 如果你想保持期间使用dt. instead of df. 而不是df. .

If you don't want to delete from the beginning of the line and instead want to do sentences, the :%s command is probably more appropriate here. 如果您不想从行的开头删除而是想要执行句子,则:%s命令可能更合适。 (eg :%s/\\(There was\\)[^.]*\\./\\1/g or %s/\\(There was\\)[^.]*\\./\\1./g if you want to keep the period at the end of the sentence. (例如:%s/\\(There was\\)[^.]*\\./\\1/g%s/\\(There was\\)[^.]*\\./\\1./g如果你想要将句号保留在句末。

使用搜索和替换

:%s/There was[^.]*\.\s*//g

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

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