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
2009-10-11 17:45:06
+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
2009-10-12 00:25:22
is there a way to silence matching errors?
UncleZeiv
2010-01-27 17:07:10
@UncleZeiv: I'm not quite sure what you mean by errors
too much php
2010-01-27 22:09:20
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
2010-01-28 14:28:20
ok, easy enough, I just had to change it to `:autocmd CursorMoved * silent! exe printf('match IncSearch /\<%s\>/', expand('<cword>'))`
UncleZeiv
2010-01-29 10:53:24