我在视觉选择和运行正则表达式替换方面遇到问题。当我选择一些不包含整行的文本,然后点击 : 来调出命令行,然后执行类似的操作
:s/T/t/
然后该行的第一个匹配项(无论是否被选择)被更改。例如,我有文字
Test Text here
我视觉上选择了这个词Text,然后运行上面的替换,我最终得到
test Text here
这不是我想要的。
有什么想法如何达到正确的结果吗?
编辑:实际的命令行是
'<,'>s/T/t/
当您按下 : 时,Vim 默认会进行视觉选择。
您可以使用 \%V (请参阅http://vimdoc.sourceforge.net/htmldoc/pattern.html#//%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.
So:
:s/\%VT/t/
如果要替换多个命中,请添加 /g
:s/\%VT/t/g
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)