2011-12-19 67 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