2013-07-23 2 views
3

vim에서 대소 문자를 구분하여 :substitute(...)을 사용하고 싶지만 그렇게하지 마십시오.어떻게해야합니까?()를 vim의 대소 문자 구분 플래그로 대체 하시겠습니까?

let s:Var = 'foo BAR baz' 

나는 물론 가 명시 적으로 다음 줄 BAR 그렇게 그 noic을 설정할 수 있습니다 여기에

내가가 조작하려는 변수 (들의 : 바르가) 대체되지 않습니다

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 

지금, 나는 그것을 senstitive 케이스 만들기 위해 :substituteI 플래그를 사용할 수 있습니다 생각하지만,이 경우 될 것 같지 않습니다

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을 대체해서는 안됩니다.

답변

5

당신이 인용 [I]에 대한 도움말 메시지는 하지substitute()에 대한 기능입니다. :s 명령입니다.

substitute() 기능의 플래그는 "g" 또는 "" 중 하나를 가질 수있다.

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

확인이 도움말 텍스트 :이 기능을 대소 문자 구분 일치를 수행하려는 경우처럼, 당신의 패턴으로 \C를 추가

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. 
+0

감사를 설명. 나는 플래그가': s'와': substitute()'사이에서 전송 가능하다고 잘못 가정했다. –

2

substitute 명령에 대한 도움말은 말한다 :

{플래그}에서 "g", {팻}의 모든 경기는

가 {EXPR} 대체됩니다. 그렇지 않으면 {flags}가 ""이어야합니다.

그래서이 경우에 {flags}에 대한 유효한 값은 "g" 또는 "" 있습니다.

그러나 검색 패턴에서 \C 플래그를 직접 사용할 수 있습니다.