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

34169Re: Visual block question

Expand Messages
  • Pavol Juhas
    Dec 12, 2003
      On Fri, Dec 12, 2003 at 08:40:19AM +0100, Miroslaw Dobrzanski-Neumann wrote:
      > Some time ago I have asked for pattern matching only in a block.
      > But I got now answer.
      > Search and replace works fine for me:
      > :'<,'>s/a/b/g
      > There seems to be no command for searching only. I've tried
      > :'<,'>search (a)
      > but but vim tells me: "search is not an editor command" althogh documented in
      > eval.txt

      Indeed, search is not a command, it is a function. To run
      function on a range of lines you need to do
      :'<,'>call search('a')
      but it would probably not do what you want - search() function does
      not accept range, so it would be executed for every line in '<,'>
      and it would find the first 'a' after '>.

      OK, here it is - save and source the following lines and try
      :1,5call RangeSearch('a') this should match 'a' in lines 1-5.

      function! RangeSearch( pat ) range
      " match on the firstline or below
      let pbeg = '\(\%' . a:firstline . 'l\|\%>' . a:firstline . 'l\)'
      " match on the lastline or above
      let pend = '\(\%' . a:lastline . 'l\|\%<' . a:lastline . 'l\)'
      let @/ = pbeg . '\&' . pend . '\&' . a:pat
      normal! n

      Application with visual block is up to you.

    • Show all 13 messages in this topic