我如何:用vim中的case敏感标志替换()?

Ren*_*ger 3 vim case-sensitive substitution

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

这是我想要操作的变量:

let s:Var = 'foo BAR baz'
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

现在,我认为我可以使用该I标志,:substitute以使其案例敏感,但似乎并非如此:

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

I国旗的帮助如下:

[I] Don't ignore case for the pattern.  The 'ignorecase' and 'smartcase'
    options are not used.
    {not in Vi}
Run Code Online (Sandbox Code Playgroud)

我对这些线的理解是,使用该标志,BAR不应该被替换.

Ken*_*ent 6

[I]您引用的帮助信息不适用于substitute() 功能.这是为了:s命令.

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

substitute(s:Var, '\Cbar', '___', '')
Run Code Online (Sandbox Code Playgroud)

查看此帮助文本:

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.
Run Code Online (Sandbox Code Playgroud)