Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto highlight the current word in vim?

Tags:

highlight

vim

For example, our text is:

hello world
abcd hello world
hello world

In eclipse, when your cursor is at some word, the word hello is auto highlight in the current file. When you type ww in normal mode, the cursor is at other word world will highlight in the current file, the hello is un-highlighted automatically. This feature is very convenient for users.

Does vim can do this with some plugin or else?

like image 590
jinleileiking Avatar asked Aug 10 '14 09:08

jinleileiking


1 Answers

Something like this?

set updatetime=10

function! HighlightWordUnderCursor()
    if getline(".")[col(".")-1] !~# '[[:punct:][:blank:]]' 
        exec 'match' 'Search' '/\V\<'.expand('<cword>').'\>/' 
    else 
        match none 
    endif
endfunction

autocmd! CursorHold,CursorHoldI * call HighlightWordUnderCursor()

This won't clobber the search register but will use the same highlighting as would normally be used. (If you want a different highlight color change Search to that highlight group.) A short update time is needed so that the CursorHold event it fired fairly often. It also won't highlight anything if the cursor is above punctuation or whitespace.

The iskeyword setting determines what is considered part of a word when expand('<cword>') is used.

like image 178
FDinoff Avatar answered Oct 25 '22 04:10

FDinoff