On 08/21/2011 11:58 PM, John Beckett wrote:
I just reworked an interesting idea added to this tip:
http://vim.wikia.com/wiki/Highlight_all_search_pattern_matches

The following script means you can press Enter to toggle search
highlighting for the current word on and off (without moving the
cursor). I did not think I would want this, but it's strangely
attractive.

let g:highlighting = 0
function! Highlighting()
   if g:highlighting == 1&&  @/ =~ '^\\<'.expand('<cword>').'\\>$'
     let g:highlighting = 0
     return ":silent nohlsearch\<CR>"
   endif
   let @/ = '\<'.expand('<cword>').'\>'
   let g:highlighting = 1
   return ":silent set hlsearch\<CR>"
endfunction
nnoremap<silent>  <expr>  <CR>  Highlighting()

Question: Can a script detect whether search highlighting is
currently active? The above uses a global variable as a
workaround

Well, you always have access to &hls as a variable, so you can do things like

  function! Highlighting()
        let l:search = '\<'.expand('<cword>').'\>'
        if &hls == 1 && @/ == l:search
                return ":silent set nohls\<cr>"
        endif
        let @/ = l:search
        return ":silent set hls\<cr>"
  endfunction
  nnoremap <silent> <expr> <cr> Highlighting()

With a little thought, one might be able to smash this into a one-liner non-<expr> mapping, but the readability of the function is a bit easier.

-tkc



  endif

--
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

Reply via email to