2011-02-01 3 views
31

vimscript에서 다음 함수를 완성하려면 어떻게해야합니까?변수에 대용품 사용

fun! Foo() 
    let l:bar = "Hello there, world!" 
    # Perform a substitution on l:bar, changing "world" to "kitten" 
endfun 

즉, 현재 버퍼가 아닌 변수에서 대체를 수행하려면 어떻게해야합니까?

나는 버퍼에 대체하기 위해, 내가

silent :%s/world/kitten/g 

을 쓸 수 있지만, 변수 대체에 해당하는 명령은 무엇인가 알아?

+0

Re : 롤백 : 태그는 제목이 아닌 프로그래밍 언어 용입니다. –

답변

39

:help substitute:help substitute()입니다.

대체 명령의 대체품입니다 (:help :substitute 참조). 당신의 예에서

substitute({expr}, {pat}, {sub}, {flags})  *substitute()* 

    The result is a String, which is a copy of {expr}, in which 
    the first match of {pat} is replaced with {sub}. 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. 'smartcase' is not used. 
    See |string-match| for how {pat} is used. 
    And a "~" in {sub} is not replaced with the previous {sub}. 
    Note that some codes in {sub} have a special meaning 
    |sub-replace-special|. For example, to replace something with 
    "\n" (two characters), use "\\\\n" or '\\n'. 
    When {pat} does not match in {expr}, {expr} is returned 
    unmodified. 
    When {flags} is "g", all matches of {pat} in {expr} are 
    replaced. Otherwise {flags} should be "". 
    Example: > 
     :let &path = substitute(&path, ",\\=[^,]*$", "", "") 
    This removes the last component of the 'path' option. 
     :echo substitute("testing", ".*", "\\U\\0", "") 
    results in "TESTING". 

나는
let l:bar = substitute(l:bar, "world", "kitten", "")

+0

내 예제에서 버퍼로'substitute'를 사용하고 있습니다; 버퍼가 아닌 변수에 적용하는 방법을 모르겠다. –

+0

@Sebasitan P. : 두 개의 대체 명령이 있습니다. 첫 번째 명령을 확인하면 대체 기능 (및 명령이 아님)에 대한 구문이 표시됩니다. –

+0

': h substitute '는 도움말 페이지와 마찬가지로' : h : 대용품 "이라고 말하면 차이점을보기가 어렵습니다. –

0

내가 내 플러그인 vim-encode을 만들 때 마법을 피하기 위해 정력 설명서를 찾아 볼 필요 싫증 일을해야한다는 생각, 여기에 일반 텍스트에 대한 순수한 vim 스크립트 구현의 발견

사용 대체 & : s:strreplace("abc","a","b") 반환 "bbc", s:strreplace("abc",["a","b"],["b","a"]) 수익을 "bac"

,
func! s:strfind(s,find,start) 
      if type(a:find)==1 
        let l:i = a:start 
        while l:i<len(a:s) 
          if strpart(a:s,l:i,len(a:find))==a:find 
            return l:i 
          endif 
          let l:i+=1 
        endwhile 
        return -1 
      elseif type(a:find)==3 
        " a:find is a list 
        let l:i = a:start 
        while l:i<len(a:s) 
          let l:j=0 
          while l:j<len(a:find) 
            if strpart(a:s,l:i,len(a:find[l:j]))==a:find[l:j] 
              return [l:i,l:j] 
            endif 
            let l:j+=1 
          endwhile 
          let l:i+=1 
        endwhile 
        return [-1,-1] 
      endif 
    endfunc 

    func! s:strreplace(s,find,replace) 
      if len(a:find)==0 
        return a:s 
      endif 
      if type(a:find)==1 && type(a:replace)==1 
        let l:ret = a:s 
        let l:i = s:strfind(l:ret,a:find,0) 
        while l:i!=-1 
          let l:ret = strpart(l:ret,0,l:i).a:replace.strpart(l:ret,l:i+len(a:find)) 
          let l:i = s:strfind(l:ret,a:find,l:i+len(a:replace)) 
        endwhile 
        return l:ret 
      elseif type(a:find)==3 && type(a:replace)==3 && len(a:find)==len(a:replace) 
        let l:ret = a:s 
        let [l:i,l:j] = s:strfind(l:ret,a:find,0) 
        while l:i!=-1 
          let l:ret = strpart(l:ret,0,l:i).a:replace[l:j].strpart(l:ret,l:i+len(a:find[l:j])) 
          let [l:i,l:j] = s:strfind(l:ret,a:find,l:i+len(a:replace[l:j])) 
        endwhile 
        return l:ret 
      endif 

    endfunc 
관련 문제