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

Example code to wrap predefined text around selected text

Expand Messages
  • Harry Putnam
    Can anyone show an example of how to do this: I want to wrap this: # [HP 101202_15:55:07
    Message 1 of 3 , Dec 2, 2010
      Can anyone show an example of how to do this:

      I want to wrap this:

      # [HP 101202_15:55:07
      <selected text region here>
      <selected text region here>
      <selected text region here>
      <selected text region here>
      # ]

      Around the selected text... that is, select some text and press a key
      combo ... voila... its no surrounded by:
      # [HP 101202_15:55:07

      # ]

      Over the yrs I've acquired a fairly lengthy .vimrc but coding
      something like that is clear out of my skil set.

      --
      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php
    • lessthanideal
      ... Try this mapping in your .vimrc to map a key for this - I used F9 map :g/ % To explain (see :help
      Message 2 of 3 , Dec 3, 2010
        On Dec 2, 9:58 pm, Harry Putnam <rea...@...> wrote:
        > Can anyone show an example of how to do this:
        >
        > I want to wrap this:
        >
        > # [HP 101202_15:55:07
        >   <selected text region here>
        >   <selected text region here>
        >   <selected text region here>
        >   <selected text region here>
        > # ]
        >
        > Around the selected text... that is, select some text and press a key
        > combo ... voila... its no surrounded by:
        >   # [HP 101202_15:55:07
        >
        >   # ]

        Try this mapping in your .vimrc to map a key for this - I used F9

        map <F9> :g/\%'</s/\%V\_.*\%V../# [HP 101202_15:55:07\r\0\r#]/<CR>

        To explain (see :help pattern for more details)

        :g/\%'</

        Do this command on the line where the "start of visual selection" mark
        is. This is necessary since if you just try the next bit as a simple
        :%s command, the marks change as the substitution happens, so it
        goes through and does more than one substitution since it find the
        marks
        more than once.

        s/\%V\_.*\%V..

        Search for as many characters including newlines that are in the
        visual selection. The .. at the end is necessary since \%V is a
        zero-width match.

        /# [HP 101202_15:55:07\r\0\r#]/

        Replace the found pattern (\0) with your text and newlines

        <CR>

        Equivalent to pressing Enter at the end if you entered the command
        yourself.

        ----------------

        I'm sure there's a better way to do this but I couldn't find it.
        My first thought was to use the begin and end of visual selection
        marks \%'< and \%'>, something like

        :%s/\%'<.*\%'>/# [HP 101202_15:55:07\r\0\r#]/

        but \%'> doesn't seem to work (just do a visual selection then
        search for /\%'> and it fails) and I couldn't figure out why.

        regards,
        Geoff

        --
        You received this message from the "vim_use" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php
      • Luc Hermitte
        Hello, ... I ve written a few functions to do surround anything: InsertAroundVisual() and Surround()
        Message 3 of 3 , Dec 3, 2010
          Hello,

          "Harry Putnam" wrote :
          > Can anyone show an example of how to do this:
          >
          > I want to wrap this:
          > [...]
          > Around the selected text... that is, select some text and press a key
          > combo ... voila... its no surrounded by:
          > # [HP 101202_15:55:07
          >
          > # ]
          >
          > Over the yrs I've acquired a fairly lengthy .vimrc but coding
          > something like that is clear out of my skil set.

          I've written a few functions to do surround anything: InsertAroundVisual() and Surround()
          http://code.google.com/p/lh-vim/wiki/lhBrackets#The_VimL_library

          And I suspect there is a vim tip on the subject.

          HTH,

          --
          Luc Hermitte
          http://lh-vim.googlecode.com/
          http://hermitte.free.fr/vim/

          --
          You received this message from the "vim_use" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php
        Your message has been successfully submitted and would be delivered to recipients shortly.