3

我想:substitute(...)在 vim 中以区分大小写的方式使用,但没有这样做。

这是我要操作的变量:

let s:Var = 'foo BAR baz'

我当然可以明确设置noic,以便在以下行BAR中(s:Var)不被替换:

set noic
let s:S1 = substitute(s:Var, 'bar', '___', '')
" print    foo BAR baz
echo s:S1 

相反,如果ic设置了,BAR当然会被替换:

set ic 
let s:S2 = substitute(s:Var, 'bar', '___', '')
" print    foo ___ baz
echo s:S2

现在,我想我可以使用I标志:substitute来使其区分大小写,但事实并非如此:

let s:S3 = substitute(s:Var, 'bar', '___', 'I')
" print   foo ___ baz
" instead of the expected foo BAR baz
echo s:S3

I标志的帮助内容如下:

[I] Don't ignore case for the pattern.  The 'ignorecase' and 'smartcase'
    options are not used.
    {not in Vi}

我对这些行的理解是,使用该标志,不应替换 BAR。

4

2 回答 2

5

[I]关于您引用的帮助信息不适用于substitute() function。这是为了:s指挥。

substitute()函数标志可以有"g"""。如果您想使用此函数进行区分大小写的匹配,请添加\C您的模式,例如:

substitute(s:Var, '\Cbar', '___', '')

检查此帮助文本:

The result is a String, which is a copy of {expr}, in which
        the first match of {pat} is replaced with {sub}.
        When {flags} is "g", all matches of {pat} in {expr} are
        replaced.  Otherwise {flags} should be "".

        This works like the ":substitute" command (without any flags).
        But the matching with {pat} is always done like the 'magic'
        option is set and 'cpoptions' is empty (to make scripts
        portable).  'ignorecase' is still relevant, use |/\c| or |/\C|
        if you want to ignore or match case and ignore 'ignorecase'.
        'smartcase' is not used.  See |string-match| for how {pat} is
        used.
于 2013-07-23T08:36:30.667 回答
2

substitute命令的帮助说:

当 {flags} 为 "g" 时,替换 {expr} 中 {pat} 的所有匹配项。否则 {flags} 应该是 ""。

{flags}因此,在这种情况下,唯一有效的值是"g"or ""

但是,\C直接在搜索模式中使用标志可能会起作用。

于 2013-07-23T08:35:08.627 回答