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?
10 Answers
This autocommand will do what you want:
:autocmd CursorMoved * exe printf('match IncSearch /\V\<%s\>/', escape(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
-
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. Jan 28, 2010 at 14:28 -
9ok, easy enough, I just had to change it to
:autocmd CursorMoved * silent! exe printf('match IncSearch /\<%s\>/', expand('<cword>'))
Jan 29, 2010 at 10:53 -
3@UncleZeiv: I have fixed the answer to escape the pattern. The "silent!" only suppresses errors - it is better to fix the cause of them.. ;)– blueyedJul 12, 2012 at 9:25
-
3Could any charitable soul explain how does this command work? Dec 30, 2012 at 6:04
-
3Netbeans would highlight the variables that accessible with the context of currently chosen variable. It knows the difference between the same variable declared within lamda function and a normal function that contained it and will highlight accordingly. Is it possible to introduce that kind of language specific intelligence in highlighting?– SenGSep 1, 2015 at 20:18
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.
-
1your solution is great! the only problem is when I use * the cursor jumps over to the next search result. Isn't it possible to keep cursor on the current word?– B FaleyMay 10, 2011 at 4:59
-
2@Meysam - you can create a simple mapping which does just that (untested): nmap <leader>* *N Feb 24, 2012 at 13:41
-
1As an addition to lukaszkorecki's comment: If you want the cursor to stay at same position you could use
nmap <leader>* ``
(doubled backtick) Sep 15, 2019 at 19:14
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
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()
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.
vim_current_word
works out of the box, is syntax aware, and allows customisable colours.
To map F2 to toggle highlighting:
map <F2> :set hlsearch!<CR> * #
It is certainly not perfect. '* #' jumps around a bit much...
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
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.
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