我想在vim中以区分大小写的方式使用:substitute(...)
,但未能这样做。
这是我想要操作的变量:
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不应该被替换。
答案 0 :(得分:5)
[I]
功能,您引用substitute()
的帮助讯息 。它适用于: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.
答案 1 :(得分:2)
substitute
命令的帮助说:
当{flags}为“g”时,{expr}中{pat}的所有匹配都将被替换。否则{flags}应为“”。
因此,此实例中{flags}
的唯一有效值为"g"
或""
。
但是,直接在搜索模式中使用\C
标志可能有效。