Alex Rivera | Logout

How to highlight all occurrences of a selected word in VIM?

Asked 2010-08-07T16:33:24.870
208

How can I highlight all occurrence of a selected word in GVim, like in Notepad++?

Edit
Report

5 Answers

260

In Normal mode:

:set hlsearch

Then search for a pattern with the command / in Normal mode, or <Ctrl>o followed by / in Insert mode. * in Normal mode will search for the next occurrence of the word under the cursor. The hlsearch option will highlight all of them if set. # will search for the previous occurrence of the word.

To remove the highlight of the previous search:

:nohlsearch

You might wish to map :nohlsearch<CR> to some convenient key.

answered 2010-08-07T16:38:36.587
158

The * key will highlight all occurrences of the word that is under the cursor.

answered 2010-08-07T16:36:42.907
11

Search based solutions (*, /...) move cursor, which may be unfortunate.

An alternative is to use enhanced mark.vim plugin, then complete your .vimrc to let double-click trigger highlighting (I don't know how a keyboard selection may trigger a command) :

"Use Mark plugin to highlight selected word  
map <2-leftmouse> \m   

It allows multiple highlightings, persistence, etc.

To remove highlighting, either :

  • Double click again
  • :Mark (switch off until next selection)
  • :MarkClear
answered 2011-10-14T18:58:12.240
3

First ensure that hlsearch is enabled by issuing the following command

:set hlsearch

You can also add this to your .vimrc file as set

set hlsearch

now when you use the quick search mechanism in command mode or a regular search command, all results will be highlighted. To move forward between results, press 'n' to move backwards press 'N'

In normal mode, to perform a quick search for the word under the cursor and to jump to the next occurrence in one command press '*', you can also search for the word under the cursor and move to the previous occurrence by pressing '#'

In normal mode, quick search can also be invoked with the

/searchterm<Enter>

to remove highlights on ocuurences use, I have bound this to a shortcut in my .vimrc

:nohl
answered 2010-08-09T04:29:30.113
2
set hlsearch

maybe ?

answered 2010-08-07T16:38:21.407

Your Answer