[ACCEPTED]-Highlight variable under cursor in Vim like in NetBeans-highlight

Accepted answer
Score: 96

This autocommand will do what you want:

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

vi highlight current word

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

:so $VIMRUNTIME/syntax/hitest.vim
Score: 24

If you set

:set hlsearch

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

Score: 9

This statement will allow a variable to 7 enable/disable highlighting all occurences 6 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 5 highlighting with:

:let HlUnderCursor=1

And disable it with:

:let HlUnderCursor=0

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

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

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

:unlet HlUnderCursor
Score: 6

If you do not want to highlight language 4 words (statements / preprocs such as if, #define) when 3 your cursor is on these words, you can put 2 this function in your .vimrc based on the @too_much_php 1 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()
Score: 5

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

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

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

Cheers.

Score: 1

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

Score: 1

To map F2 to toggle highlighting:

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

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

Score: 0

This variant is optimized for speed (uses 3 CursorHold instead of CursorMoved) and compatibility 2 with hlsearch. The current search word highlighting 1 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
Score: 0

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

set updatetime=1000

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

See :h CursorHold for more info.

Score: 0

vim-illuminate does the trick for me.

match-up does the trick for me.

vim match-up: even 2 better % navigate and highlight matching 1 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

More Related questions