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

Re: Copying matched parts of lines

Expand Messages
  • Tim Chase
    ... I believe Christian s comment about the recent enough Vim involves the /n flag actually executing the replacement if it s a sub-replace-special (earlier
    Message 1 of 4 , Mar 26, 2013
      On 2013-03-26 16:56, Christian Brabandt wrote:
      > On Tue, March 26, 2013 16:43, BPJ wrote:
      > > Is it somehow possible with :g// to copy only the
      > > matched parts of lines as with the -o option of grep?
      > >
      > > /bpj
      > With a recent enough Vim, you can make use of :s command and
      > execute VimL on the replacement part while not modifying the match
      > (by using the 'n' flag):
      > fu! CopyMatches(match)
      > if !exists("g:a")
      > let g:a = []
      > endif
      > call add(g:a, a:match)
      > endfu
      > :%s/foobar/\=CopyMatches(submatch(0))/gn
      > :new +put\ =g:a

      I believe Christian's comment about the "recent enough Vim" involves
      the /n flag actually executing the replacement if it's a
      sub-replace-special (earlier versions didn't, IIRC). If that's the
      case, the function can be modified to simply return the match:

      fu! CopyMatches(match)
      if !exists("g:a")
      let g:a=[]
      call add(g:a, a:match)
      return a:match

      It may have the unfortunate side-effect of setting 'modified' when all
      the modifications were NOOPs, but you can either simply undo the
      non-modification, or you can force the setting of 'nomodified'.


      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

      You received this message because you are subscribed to the Google Groups "vim_use" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
      For more options, visit https://groups.google.com/groups/opt_out.
    Your message has been successfully submitted and would be delivered to recipients shortly.