--- Robert Cussons <[EMAIL PROTECTED]> wrote:
> Jean-Rene David wrote:
> > * Robert Cussons [2006.10.18 09:29]:
> > 
> >>Everything seems to work fine now, except the
> >>searched for items aren't highlighted like they
> >>normally are when I search
> > 
> > 
> > Whether or not search items are highlighted
> > depends on the value of the 'hlsearch' option.
> > 
> > The search item gets highlighted on my end when the
> > option is set. Is yours set?
[snip]

I have learned a lot from this thread!

I changed the function slightly (see below) because I also
didn't understand the missing ^M was causing my problem.

I like to use * and # for searching for a string in highlight
mode, then I press F10 to turn off highlighting.  However,
pressing F10 with nohls highlights the word under the cursor
without searching for it, which was is a modification of some
comments at the bottom of vimtip #1.

Between this thread and the discussions in vimtip #1 I now
have the following which works really *really* nicely:

"----begin----
" disabled initially since it distracts me
set nohlsearch

" in normal mode, make * highlight the search string
nnoremap <silent> * *:set hls<CR>
nnoremap <silent> # #:set hls<CR>

" in visual mode, use highlighted selection for search pattern
vnoremap <silent> * :call VisualSearch('f')<CR>:set hls<CR>
vnoremap <silent> # :call VisualSearch('b')<CR>:set hls<CR>
function! VisualSearch(direction)
    let l:saved_reg = @"
    execute "normal! vgvy"
    let l:pattern = escape(@", '\/.*$^~[]')
    let l:pattern = substitute(l:pattern, "\n$", "", "")
    let @/ = l:pattern
    let @" = l:saved_reg
    if a:direction == 'f'
        normal n
    else
        normal N
    endif
endfunc

" map <F10> to toggle the highlight search mode
" if hlsearch is on  then simply turn it off
" if hlsearch is off then highlight word under cursor only
nnoremap <silent> <F10> :call SetSearchReg()<CR>:set invhls<CR>
function! SetSearchReg()
    if &hlsearch == 0
        let @/ = expand('<cword>')
    endif
endfunc

" my mind/fingers think of <F10> as highlight search mode
" so make visual mode <F10> and <Shift><F10> work like * and #
vnoremap <silent> <F10> :call VisualSearch('f')<CR>:set hls<CR>
vnoremap <silent> <S-F10> :call VisualSearch('b')<CR>:set hls<CR>
"-----end-----

Regards,

David


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Reply via email to