tags:

views:

49

answers:

2

Hello

I am looking for an elegant solution to (within vim script) iterate over all matches of a regular expression in a buffer. That would be something like

fu! DoSpecialThingsWithMatchedLines()

  for matched_line_no in (GetMatchedPositions("/foo\\>.*\\<bar"))
      let line = getline(matched_line_no)
      call DoItPlease(line)
  end for

endfu

Is there something like this? I am not necessarily looking for a full fledged solution, any pointer directing me into the right direction will do.

Thanks / Rene

+1  A: 

you could use :he :global eg.

 :%g/foo\\>.*\\<bar/call DoItPlease(getline("."))

vimscript example:

fun! Doit(line)
    echo a:line
endfun


fun! MyDo()
    %g/foo/call Doit(getline("."))
endfun

:call MyDo()
michael
This approach certainly meets what I functionally want(ed). Yet, I hoped there would be a for .. in construct so that I visually can see what's going on.
René Nyffenegger
no worries. file it away for the future. It is very handy
michael
+1  A: 

Most of the time I'd use michael's solution with :global

You can also play with filter(getline('1','$'), 'v:val =~ "foo\\>.*\\<bar"') if you really want to use :for.

Otherwise, you can simply call search() in a loop.

Luc Hermitte