On Dec 1, 11:12 am, Christian Brabandt <[email protected]> wrote: > Hi Peng! > > On Di, 01 Dez 2009, Peng Yu wrote: > > > I frequently need to switch two strings. For example, if I want to > > switch 'a' and 'b', I shall use the following three commands. I'm > > wondering if there is a more convenient way to do so. > > > :%s/b/c/g > > :%s/a/b/g > > :%s/c/a/g > > fu! <SID>SwapWords(...) range > if len(a:000)!= 2 > echomsg "SwapWords has to be called with 2 Arguments!" > return > elseif (search(a:1 ,'n') == 0 || search(a:2, 'n') == 0) > echomsg "not both args found" > return > endif > > let pat=join(map(copy(a:000), '"\\%(" .v:val. "\\)"'), '\|') > :exe a:firstline . ',' . a:lastline . 's/'.pat. > '/\=printf("%s",submatch(0)==a:1 ? a:2 : a:1 )/' > endfu > > com! -nargs=+ -range=% Swap :<line1>,<line2>call <SID>SwapWords(<f-args>) > > :Swap a b
I try the above command in a file that has on the following line. a b But it only replaces 'a' with 'b' but not 'b' with 'a'. -- You received this message from the "vim_use" maillist. For more information, visit http://www.vim.org/maillist.php
