Wednesday, December 2, 2009

Re: How to switch two strings?

Why not use macro?

2009/12/2 Christian Brabandt <cblists@256bit.org>:
> On Wed, December 2, 2009 3:14 am, Peng Yu wrote:
>>
>>
>> On Dec 1, 11:12 am, Christian Brabandt <cbli...@256bit.org> 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'.
>
> Oh yes,
> Change this line:
> :exe a:firstline . ',' . a:lastline .
> 's/'.pat.'/\=printf("%s",submatch(0)==a:1 ? a:2 : a:1 )/'
>
> to
>
> :exe a:firstline . ',' . a:lastline .
> 's/'.pat.'/\=printf("%s",submatch(0)==a:1 ? a:2 : a:1 )/g'
>
> regards,
> Christian
>
> --
> You received this message from the "vim_use" maillist.
> For more information, visit http://www.vim.org/maillist.php

--
---- 可移植文档格式 ------------

PDF 格式是数字化文档事实上的工业标准,它可以将文字、
字型、格式、颜色及独立于设备和分辨率的图形图像等封装在
一个文件中。

该格式文件还可以包含超文本链接、声音和影像等信息,支持特长
文件。PDF 格式使用了工业标准的压缩算法,易于传输与储存。

PDF 格式的文档具有纸版书的质感和阅读效果,能"逼真"展现书
的原貌,显示大小可任意调节,完全不依赖操作系统的语言和
字体及显示设备。

--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments: