【问题标题】:Vim visual selection and regexpVim 视觉选择和正则表达式
【发布时间】:2011-12-19 13:40:41
【问题描述】:

我在视觉选择和运行正则表达式替换时遇到问题。当我选择一些不包含整行的文本,然后点击 : 以调出命令行,并执行类似的操作

:s/T/t/

然后行上的第一个匹配项(无论它是否被选中)被更改。所以,例如,我有文字

Test Text here

我在视觉上选择了Text这个词,然后运行上面的替换,我最终得到了

test Text here

这不是我想要的。

任何想法如何达到正确的结果?

编辑:实际的命令行是

'<,'>s/T/t/

当你按下 : 时,Vim 会默认设置。

【问题讨论】:

    标签: vim


    【解决方案1】:

    你可以使用\%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
    

    【讨论】:

    • 根据:h %V,如果要确保模式在选择范围内,则应将整个搜索模式包装在\%V 中。喜欢::s/\%Vlongpattern\%V/short/.
    • @m0tive:我确实引用了相关文档。而且,当模式是无用的单个字符时
    • 你对我来说太快了 ;-)
    • 谢谢。我的实际模式不止一个字符,所以我需要了解 \%V。
    猜你喜欢
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    相关资源
    最近更新 更多