On Wed, Oct 18, 2006 at 12:28:28PM +0200, Robert Cussons wrote:
> Jean-Rene David wrote:
[snip]
> >
> >"--------------< cut here >---------------------------
> >" Search for visually selected text {{{
> >" From an idea by Michael Naumann, Jürgen Krämer.
> >function! VisualSearch(direction) range
> >   let l:saved_reg = @"
> >   execute "normal! vgvy"
> >   let l:pattern = escape(@", '\\/.*$^~[]')
> >   let l:pattern = substitute(l:pattern, "\n$", "", "")
> >   if a:direction == 'b'
> >      execute "normal ?" . l:pattern . "
> >"
> >   else
> >      execute "normal /" . l:pattern . "
> >"
> >   endif
> >   let @/ = l:pattern
> >   let @" = l:saved_reg
> >endfunction
> >
> >vnoremap <silent> * :call VisualSearch('f')<CR>
> >vnoremap <silent> # :call VisualSearch('b')<CR>
> >"--------------< cut here >---------------------------
> 
> This sounds brilliant Jean-Rene, I put it into my .vimrc, but it doesn't 
> seem to work, I did notice that between the if and else there are " 
> which just act as comments as they are on newlines, didn't know if this 
> was just my text wrapping, so I tried putting them on the line above, 
> both with a space between or not, didn't know if that might be cause but 
> that didn't seem to help either. Sorry I may be missing something 
> obvious, but I'd like to get this to work as it seems very useful.
> I was selecting text in visual mode, then pressing / or ? and I just get 
> the normal action of pressing / or ?

     I think the original included raw CR characters in the two :execute
lines.  Both are intended to end with "^M" (which is how they appeared
in my copy of Jean-Rene's note).  I think your e-mail client broke the
lines, leading to syntax errors.

     I try to avoid such problems by not including raw CR, ESC, etc.
characters in my vim scripts.  I suggest replacing the two :execute
lines with
     execute "normal ?" . l:pattern . "\<CR>"
and
     execute "normal /" . l:pattern . "\<CR>"

HTH                                     --Benji Fisher

Reply via email to