Loading ...
Sorry, an error occurred while loading the content.

Re: How to switch two strings?

Expand Messages
  • Christian Brabandt
    ... Oh yes, ... s/ .pat. / =printf( %s ,submatch(0)==a:1 ? a:2 : a:1 )/ to ... s/ .pat. / =printf( %s ,submatch(0)==a:1 ? a:2 : a:1 )/g regards, Christian
    Message 1 of 7 , Dec 1, 2009
    • 0 Attachment
      On Wed, December 2, 2009 3:14 am, Peng Yu wrote:
      >
      >
      > On Dec 1, 11:12 am, Christian Brabandt <cbli...@...> 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
    • redspider red
      Why not use macro? ... -- ... PDF ¸ñʽÊÇÊý×Ö»¯ÎĵµÊÂʵÉϵĹ¤Òµ±ê×¼£¬Ëü¿ÉÒÔ½«ÎÄ×Ö¡¢
      Message 2 of 7 , Dec 2, 2009
      • 0 Attachment
        Why not use macro?

        2009/12/2 Christian Brabandt <cblists@...>:
        > On Wed, December 2, 2009 3:14 am, Peng Yu wrote:
        >>
        >>
        >> On Dec 1, 11:12 am, Christian Brabandt <cbli...@...> 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
      • Christian Brabandt
        ... Because I like the flexibility, that a User command provides ( It has a name I can remember, allows to use tab completion and is more intuitive to use).
        Message 3 of 7 , Dec 2, 2009
        • 0 Attachment
          On Thu, December 3, 2009 2:21 am, redspider red wrote:
          > Why not use macro?

          Because I like the flexibility, that a User command provides (
          It has a name I can remember, allows to use tab completion and
          is more intuitive to use).
          Plus I personally am too lazy to record and replay a macro each
          time I need it and generally think, user-defined functions are
          easier to understand.

          Oh and BTW: Please don't top post.

          regards,
          Christian
          --
          :wq

          --
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
        Your message has been successfully submitted and would be delivered to recipients shortly.