2011-12-19 66 views
6

我遇到了視覺選擇和運行正則表達式替換的問題。當我選擇不包含整行,擊出了一些文字:把命令行了,這樣做Vim視覺選擇和正則表達式

:s/T/t/ 

然後第一場比賽就行了(無論是選擇與否)改變。因此,舉例來說,我有文字

Test Text here 

,我直觀地選擇字文本,然後運行上面的替代,我結束了

test Text here 

這不是我想要的。

任何想法如何實現正確的結果?

編輯:實際的命令行是

'<,'>s/T/t/ 

,Vim也把默認當你按下:用視覺的選擇。

回答

10

您可以使用\%V(見http://vimdoc.sourceforge.net/htmldoc/pattern.html#//%V

\%V Match inside the Visual area. When Visual mode has already been 
    stopped match in the area that |gv| would reselect. 
    This is a |/zero-width| match. To make sure the whole pattern is 
    inside the Visual area put it at the start and end of the pattern, 
    e.g.: 
     /\%Vfoo.*bar\%V 
    Only works for the current buffer. 

所以:

:s/\%VT/t/ 

如果您要更換多次點擊,添加/ G

:s/\%VT/t/g 
+1

根據':h%V',你應該包裹整個s如果你想確保模式在選擇區內,可以在'\%V'中搜索模式。像:':s/\%Vlongpattern \%V/short /'。 – m0tive 2011-12-19 13:50:01

+0

@ m0tive:我沒有引用關於該文檔。並且,當模式是單個字符時,這對您沒有用 – sehe 2011-12-19 13:50:58

+0

您對我而言太快了;-) – m0tive 2011-12-19 13:52:10