nvim-config

Log | Files | Refs | Submodules | README

swapword.vim (745B)


      1 " Source : https://stackoverflow.com/a/3579112
      2 " Usage : :call SwapWords({'foo':'bar'})
      3 " Si / dans le mot: :call SwapWords({'foo/bar':'foo/baz'}, '@')
      4 
      5 function! s:Mirror(dict)
      6     for [key, value] in items(a:dict)
      7         let a:dict[value] = key
      8     endfor
      9     return a:dict
     10 endfunction
     11 
     12 function! SwapWords(dict, ...) range
     13     let words = keys(a:dict) + values(a:dict)
     14     let words = map(words, 'escape(v:val, "|")')
     15     if(a:0 == 1)
     16         let delimiter = a:1
     17     else
     18         let delimiter = '/'
     19     endif
     20     let pattern = '\v(' . join(words, '|') . ')'
     21     exe a:firstline . ',' . a:lastline . 's' . delimiter . pattern . delimiter
     22         \ . '\=' . string(s:Mirror(a:dict)) . '[submatch(0)]'
     23         \ . delimiter . 'g'
     24 endfunction
     25