Vim Tips Wiki
(→‎Comments: question on recent edit)
(Change <tt> to <code>, perhaps also minor tweak.)
(5 intermediate revisions by 4 users not shown)
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
Vim can easily [[highlight all search pattern matches]] and [[Search|search for the current word]] (the word under the cursor). This tip shows how to automatically highlight all occurrences of the current word without searching. That can useful when examining unfamiliar source code: just move the cursor to a variable, and all occurrences of the variable will be highlighted.
+
Vim can easily [[highlight all search pattern matches]] and [[Search|search for the current word]] (the word under the cursor). This tip shows how to automatically highlight all occurrences of the current word without searching. That can be useful when examining unfamiliar source code: just move the cursor to a variable, and all occurrences of the variable will be highlighted.
   
 
==Script==
 
==Script==
Put the following code in your [[vimrc]], or create file <tt>~/.vim/plugin/autohighlight.vim</tt> (Unix) or <tt>$HOME/vimfiles/plugin/autohighlight.vim</tt> (Windows) containing the script below. Then restart Vim.
+
Put the following code in your [[vimrc]], or create file <code>~/.vim/plugin/autohighlight.vim</code> (Unix) or <code>$HOME/vimfiles/plugin/autohighlight.vim</code> (Windows) containing the script below. Then restart Vim.
   
To automatically highlight the current word, type <tt>z/</tt>. To turn off, type <tt>z/</tt> again.
+
To automatically highlight the current word, type <code>z/</code>. To turn off, type <code>z/</code> again.
 
<pre>
 
<pre>
 
" Highlight all instances of word under cursor, when idle.
 
" Highlight all instances of word under cursor, when idle.
Line 44: Line 44:
 
==Comments==
 
==Comments==
 
===Escaping===
 
===Escaping===
A recent edit ([http://vim.wikia.com/index.php?title=Auto_highlight_current_word_when_idle&diff=29630&oldid=27148 here]) introduced <tt>escape()</tt> into the previous line (which was {{tt|1=au CursorHold * let @/ = '\<'.expand('<cword>').'\>'}}). In general the escape is a good idea, but in practice the current word (cword) is unlikely to have punctuation in it that needs escaping. Thoughts? [[User:JohnBeckett|JohnBeckett]] 07:11, March 3, 2011 (UTC)
+
A recent edit ([http://vim.wikia.com/index.php?title=Auto_highlight_current_word_when_idle&diff=29630&oldid=27148 here]) introduced <code>escape()</code> into the previous line (which was <code>au CursorHold * let @/ = '\<'.expand('<cword>').'\>'</code>). In general the escape is a good idea, but in practice the current word (cword) is unlikely to have punctuation in it that needs escaping. Thoughts? [[User:JohnBeckett|JohnBeckett]] 07:11, March 3, 2011 (UTC)
  +
:I note that <cword> works depending on the 'isk' setting, which for some filetypes (very notably, help files) could contain a backslash. I note that the backslash is the only character escaped in the addition. Since we are using the "very no magic" \V modifier, this is really the only character that could possibly need escaping. I think we should keep it, but explain why. --[[User:Fritzophrenic|Fritzophrenic]] 15:30, March 3, 2011 (UTC)
  +
  +
===Load at startup===
  +
How can I load this at startup without a mapping?
  +
:Either put a <code>call AutoHighlightToggle()</code> into your .vimrc after defining the function, or just grab the "guts" of the function which turn the highlight on, and add them directly to your .vimrc. The latter would be especially useful if you just want the behavior always on. --[[User:Fritzophrenic|Fritzophrenic]] 16:57, June 20, 2011 (UTC)

Revision as of 12:32, 15 July 2012

Tip 572 Printable Monobook Previous Next

created 2003 · complexity basic · author mosh · version 6.0


Vim can easily highlight all search pattern matches and search for the current word (the word under the cursor). This tip shows how to automatically highlight all occurrences of the current word without searching. That can be useful when examining unfamiliar source code: just move the cursor to a variable, and all occurrences of the variable will be highlighted.

Script

Put the following code in your vimrc, or create file ~/.vim/plugin/autohighlight.vim (Unix) or $HOME/vimfiles/plugin/autohighlight.vim (Windows) containing the script below. Then restart Vim.

To automatically highlight the current word, type z/. To turn off, type z/ again.

" Highlight all instances of word under cursor, when idle.
" Useful when studying strange source code.
" Type z/ to toggle highlighting on/off.
nnoremap z/ :if AutoHighlightToggle()<Bar>set hls<Bar>endif<CR>
function! AutoHighlightToggle()
  let @/ = ''
  if exists('#auto_highlight')
    au! auto_highlight
    augroup! auto_highlight
    setl updatetime=4000
    echo 'Highlight current word: off'
    return 0
  else
    augroup auto_highlight
      au!
      au CursorHold * let @/ = '\V\<'.escape(expand('<cword>'), '\').'\>'
    augroup end
    setl updatetime=500
    echo 'Highlight current word: ON'
    return 1
  endif
endfunction

Comments

Escaping

A recent edit (here) introduced escape() into the previous line (which was au CursorHold * let @/ = '\<'.expand('<cword>').'\>'). In general the escape is a good idea, but in practice the current word (cword) is unlikely to have punctuation in it that needs escaping. Thoughts? JohnBeckett 07:11, March 3, 2011 (UTC)

I note that <cword> works depending on the 'isk' setting, which for some filetypes (very notably, help files) could contain a backslash. I note that the backslash is the only character escaped in the addition. Since we are using the "very no magic" \V modifier, this is really the only character that could possibly need escaping. I think we should keep it, but explain why. --Fritzophrenic 15:30, March 3, 2011 (UTC)

Load at startup

How can I load this at startup without a mapping?

Either put a call AutoHighlightToggle() into your .vimrc after defining the function, or just grab the "guts" of the function which turn the highlight on, and add them directly to your .vimrc. The latter would be especially useful if you just want the behavior always on. --Fritzophrenic 16:57, June 20, 2011 (UTC)