2012-03-09 71 views
1

我有这样六,替换文本

template 
template 
template_results 
template 

文本,我需要它替换到这个

template_form 
template_form 
template_results 
template_form 

我怎么能代替template每场比赛未后跟_字符VI?

我试图像这样

:%s/template[^_]/template_form - Pattern not found 
:%s/template\[^_]/template_form - Pattern not found 
:%s/template[_]/template_form  - This works, but the pattern is opposite of what I need 

谢谢:)

+0

GregHNZ提供了完整的答案。你也可以匹配'template [^ _] *'。 – Andre 2012-03-09 08:34:42

回答

2

使用负前瞻:

:%s/template\(_\)\@!/template_form/gc 

这意味着匹配任何 “模板” 不跟随(\@!)任何 “_”

参见::help /zero-width

0

这是很容易在VIM,但坚持什么,我认为是在香草VI,你可以做

:%s/template$/template_form/ 

这假设每一行后面都有一个行尾。如果没有,请尝试:

:s/template\(\s\|$\)/template_form/ 

我不知道,如果“魔术师”是适合您的口味VI,但这种匹配的“模板”,然后空格或结束行。

2

你试图指定模板那么没有 因此,关键是用行符结束$

:%s/template$/template_form/g 

第一个不起作用,因为这[^_]匹配任何单个字符不是下划线,但不能没有字符(或行结束,显然) 。

+0

实际上,我有我需要重命名的变量,它可以跟着其他命令,如 '$ template-> SetTag();'或'$ template = new TEMPLATE;' 因此,我不能使用“结束行说明符“ – Buksy 2012-03-09 09:01:26