2012-03-09 89 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