2011-02-01 12 views
31

पर विकल्प का उपयोग करना मैं विस्क्रिप्ट में निम्न फ़ंक्शन को पूरा करने के बारे में कैसे जाउंगा?एक चर

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

पुन: रोलबैक: टैग प्रोग्रामिंग भाषा को चिह्नित करने के लिए हैं, शीर्षक नहीं। –

उत्तर

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

मैं बफर के साथ अपने उदाहरण में 'विकल्प 'का उपयोग कर रहा हूं; हालांकि मुझे नहीं पता कि बफर के बजाए इसे एक चर में कैसे लागू किया जाए। –

+0

@ सेसासिटन पी।: 2 विकल्प कमांड हैं, यदि आप पहली बार जांचते हैं तो मुझे विकल्प समारोह (और कमांड नहीं) –

+0

': एच विकल्प' के लिए सिंटैक्स मिलेगा, मुझे सहायता पृष्ठ के रूप में ' : एच: विकल्प ', इसलिए अंतर देखने के लिए मेरे लिए मुश्किल है। –

0

मैं vim मैनुअल जब मेरे प्लगइन vim-encode बनाने जादू से बचने के लिए देखने के लिए होने से थक, यहाँ सादे पाठ के लिए एक शुद्ध vimscript कार्यान्वयन खोजने &

प्रयोग की जगह: 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