tags:

views:

606

answers:

2

i worked in netbeans and liked its feature - when you place cursor in a variable name all occurences of the variable are highlighted. this is very useful for quick searching all occurences of the variable. is it possible to add this behavior to Vim ?

+5  A: 

If you set

:set hlsearch

to highlight all occurrences of a search pattern, and then use * or # to find occurrences of the word under your cursor, that will get you some way to what you want. However I think a syntax-aware variable highlighting is beyond the scope of VIM.

Brian Agnew
+10  A: 

This autocommand will do what you want:

:autocmd CursorMoved * exe printf('match IncSearch /\<%s\>/', expand('<cword>'))

Edit: I have used the IncSearch highlight group in my example, but you can find other colours to use by running this command:

:so $VIMRUNTIME/syntax/hitest.vim
too much php
is there a way to silence matching errors?
UncleZeiv
@UncleZeiv: I'm not quite sure what you mean by errors
too much php
if I move the cursor on the end of a C-style comment (star followed by slash), I get `E488: Trailing characters: match IncSearch /\<*/\>`, and I even have to press Enter before continuing, pretty annoying! I guess it should be escaped somehow and/or at least it would be ok to find a way to silence the warning. Great tip anyway.
UncleZeiv
ok, easy enough, I just had to change it to `:autocmd CursorMoved * silent! exe printf('match IncSearch /\<%s\>/', expand('<cword>'))`
UncleZeiv