Highlight variable under cursor in Vim like in NetBeans

VimNetbeansHighlight

Vim Problem Overview


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?

Vim Solutions


Solution 1 - Vim

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

Solution 2 - Vim

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.

Solution 3 - Vim

This statement will allow a variable to enable/disable highlighting all occurences of the word under the cursor:

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

One would activate highlighting with:

:let HlUnderCursor=1

And disable it with:

:let HlUnderCursor=0

One could easily define a shortcut key for enabling/disabling highlighting:

:nnoremap <silent> <F3> :exe "let HlUnderCursor=exists(\"HlUnderCursor\")?HlUnderCursor*-1+1:1"<CR>

Deleting the variable would prevent the match statement from executing, and not clear the current highlight:

:unlet HlUnderCursor

Solution 4 - Vim

If you do not want to highlight language words (statements / preprocs such as if, #define) when your cursor is on these words, you can put this function in your .vimrc based on the @too_much_php answer :

let g:no_highlight_group_for_current_word=["Statement", "Comment", "Type", "PreProc"]
function s:HighlightWordUnderCursor()
    let l:syntaxgroup = synIDattr(synIDtrans(synID(line("."), stridx(getline("."), expand('<cword>')) + 1, 1)), "name")

    if (index(g:no_highlight_group_for_current_word, l:syntaxgroup) == -1)
        exe printf('match IncSearch /\V\<%s\>/', escape(expand('<cword>'), '/\'))
    else
        exe 'match IncSearch /\V\<\>/'
    endif
endfunction

autocmd CursorMoved * call s:HighlightWordUnderCursor()

Solution 5 - Vim

i think that what you really want is the following plugin by Shuhei Kubota:

http://www.vim.org/scripts/script.php?script_id=4306

According to the description: 'This script highlights words under the cursor like many IDEs.'

Cheers.

Solution 6 - Vim

vim_current_word works out of the box, is syntax aware, and allows customisable colours.

Solution 7 - Vim

To map F2 to toggle highlighting:

map <F2> :set hlsearch!<CR> * #

It is certainly not perfect. '* #' jumps around a bit much...

Solution 8 - Vim

This variant is optimized for speed (uses CursorHold instead of CursorMoved) and compatibility with hlsearch. The current search word highlighting will not be disrupted.

" autosave delay, cursorhold trigger, default: 4000ms
setl updatetime=300

" highlight the word under cursor (CursorMoved is inperformant)
highlight WordUnderCursor cterm=underline gui=underline
autocmd CursorHold * call HighlightCursorWord()
function! HighlightCursorWord()
    " if hlsearch is active, don't overwrite it!
    let search = getreg('/')
    let cword = expand('<cword>')
    if match(cword, search) == -1
        exe printf('match WordUnderCursor /\V\<%s\>/', escape(cword, '/\'))
    endif
endfunction

Solution 9 - Vim

Similar to the accepted answer, but this way allows you to set a delay time after holding the cursor over a word before the highlighting will appear. The 1000 is in milliseconds and means that it will highlight after 1 second.

set updatetime=1000

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

See :h CursorHold for more info.

Solution 10 - Vim

vim-illuminate does the trick for me.

match-up does the trick for me.

> vim match-up: even better % navigate and highlight matching words modern matchit and matchparen > ## Features > - jump between matching words > - jump to open & close words > - jump inside (z%) > - full set of text objects > - highlight (), [], & {} > - highlight all matching words > - display matches off-screen > - show where you are (breadcrumbs) > - (neovim) tree-sitter integration

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkipelovetsView Question on Stackoverflow
Solution 1 - Vimtoo much phpView Answer on Stackoverflow
Solution 2 - VimBrian AgnewView Answer on Stackoverflow
Solution 3 - VimTjaartView Answer on Stackoverflow
Solution 4 - VimFunkySayuView Answer on Stackoverflow
Solution 5 - VimkostasvView Answer on Stackoverflow
Solution 6 - VimTom HaleView Answer on Stackoverflow
Solution 7 - Vimuser3555835View Answer on Stackoverflow
Solution 8 - VimTheJJView Answer on Stackoverflow
Solution 9 - Vimapostl3polView Answer on Stackoverflow
Solution 10 - VimTunView Answer on Stackoverflow