Highlight variable under cursor in Vim like in NetBeans

kipelovets picture kipelovets · Oct 11, 2009 · Viewed 20.3k times · Source

I worked in NetBeans and liked this 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?

Answer

too much php picture too much php · Oct 12, 2009

This autocommand will do what you want:

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

vi highlight current word

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