gpt4 book ai didi

vim - 我如何 :substitute() with the case sensitive flag in vim?

转载 作者:行者123 更新时间:2023-12-04 18:27:07 26 4
gpt4 key购买 nike

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

这是我要操作的变量:

let s:Var = 'foo BAR baz'

我当然可以明确设置 noic所以在以下几行 BAR (of 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。

最佳答案

关于[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.

关于vim - 我如何 :substitute() with the case sensitive flag in vim?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17805002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com