使用 vi 替换包含特定字符的行

Replace a line containing certain characters using vi

我想用 "CreateTime=2012-01-04 00:00" 替换所有包含 "CreateTime=xxxxx" 的行。我可以知道我应该如何使用 vim 吗?

[m18]
Attendees=38230,92242,97553
Duration=2
CreateTime=2012-01-09 22:00

[m20]
Attendees=52000,50521,34025
Duration=2
CreateTime=2012-01-09 00:00

[m22]
Attendees=95892,23689
Duration=2
CreateTime=2012-01-08 17:00

您可以为此使用全局替换运算符。

:%s/CreateTime=.*$/CreateTime=2012-01-04 00:00/g

您可以在 Vim 中使用以下方法阅读 s 命令的帮助:

:help :s

您可以阅读 :help pattern-overview 的模式。

应要求,更多关于正则表达式匹配的内容 (CreateTime=.*$):

CreateTime=  # this part is just a string
.            # "." matches any character
*            # "*" modifies the "." to mean "0 or more" of any character
$            # "$" means "end of line"

合起来,它匹配 CreateTime= 后跟任何字符系列,消耗该行的其余部分。