3

vimで大文字と小文字を区別して使いたいのですが、うまくいき:substitute(...)ません。

操作したい変数は次のとおりです。

let s:Var = 'foo BAR baz'

もちろん、次の行(s:Var の) が置換されないように明示的に設定することもできます。noicBAR

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]引用したヘルプ メッセージはfunction用ではありません。コマンド用です。substitute() :s

substitute()functionのフラグには、 または のいずれかを指定でき"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 に答える