How can I highlight all occurrence of a selected word in GVim, like in Notepad++?
16 Answers
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.

- 57,473
- 20
- 96
- 131
-
44If you plan on toggling `hlsearch` a lot you might want to map `:set hlsearch!` instead of `:set nohlsearch`. This toggles the setting rather than always turning it off. – David Winslow Aug 08 '10 at 01:23
-
1Also, be aware that the cursor will move to the previous (#) or next (*) occurrence of the word – Juan Campa Jul 30 '13 at 21:40
-
1But you can go back to the position where you pressed `*` or `#` by `
o` in normal mode. – Ruslan Feb 10 '15 at 10:57 -
11Instead of `:nohlsearch`, you can just use `:noh` – crisron Nov 29 '15 at 10:18
-
2Set in `.vimrc`: `noremap
:set hlsearch! hlsearch? – Chalist Jun 20 '17 at 02:12` for toggle between highlight and remove highlight by ` `.
The *
key will highlight all occurrences of the word that is under the cursor.

- 7,926
- 3
- 27
- 38
-
37
-
13It will also jump to the next occurrence of the word under the cursor. If you want to jump to the previous occurrence, use `#`. – Vinicius Braz Pinto Sep 22 '14 at 22:52
-
13
-
@nn0p Yes you can – in that case follow the accepted answer http://stackoverflow.com/a/3431203/363688 . This is just an alternate action that will also highlight all occurrences. – sleepynate Dec 14 '16 at 22:21
-
1is there a way to use this functionality but with the highlighted one or more words? – mc9 Aug 06 '18 at 01:27
-
@sleepynate Can we map `*` to another key/combo which doesn't require holding `shift+7` together? Lazy developer here! – Amir Aug 18 '20 at 13:56
-
How can we remove the color from the highlighted word & come back to normal? – pankaj Oct 12 '20 at 14:42
-
@pankaj See https://stackoverflow.com/a/25787211/845034. You could map toggling search result highlighting to some key, like `:nno
:set hls! – steffen Apr 17 '21 at 11:48`. -
@Amir See previous comment. Example: `:nno
*` and `:nno – steffen Apr 17 '21 at 11:54#` would search forward with `Ctrl`+`n` and backwards with `Ctrl`+`p`. -
I think what @nn0p means (because that's also my question) is, how to highlight all occurrences of *the word under the cursor* without moving the cursor. `*#` is not a good idea because it would redraw the screen, i.e. I may need to `zz`/`z-` again to get the original view. So, any answer to this? – SOFe May 26 '22 at 06:23
I know than it's a really old question, but if someone is interested in this feature, can check this code http://vim.wikia.com/wiki/Auto_highlight_current_word_when_idle
" Highlight all instances of word under cursor, when idle.
" Useful when studying strange source code.
" Type z/ to toggle highlighting on/off.
nnoremap z/ :if AutoHighlightToggle()<Bar>set hls<Bar>endif<CR>
function! AutoHighlightToggle()
let @/ = ''
if exists('#auto_highlight')
au! auto_highlight
augroup! auto_highlight
setl updatetime=4000
echo 'Highlight current word: off'
return 0
else
augroup auto_highlight
au!
au CursorHold * let @/ = '\V\<'.escape(expand('<cword>'), '\').'\>'
augroup end
setl updatetime=500
echo 'Highlight current word: ON'
return 1
endif
endfunction

- 2,224
- 2
- 21
- 33
-
1Is it possible to modify this script such that the text under the cursor that is initially highlighted when z/ is pressed stays highlighted even when the cursor is moved to other words? That would be useful when scrolling through the source code looking for the initial word. – stensootla Aug 02 '17 at 11:57
the simplest way, type in normal mode *
I also have these mappings to enable and disable
"highligh search enabled by default
set hlsearch
"now you can toggle it
nnoremap <S-F11> <ESC>:set hls! hls?<cr>
inoremap <S-F11> <C-o>:set hls! hls?<cr>
vnoremap <S-F11> <ESC>:set hls! hls?<cr> <bar> gv
Select word by clickin on it
set mouse=a "Enables mouse click
nnoremap <silent> <2-LeftMouse> :let @/='\V\<'.escape(expand('<cword>'), '\').'\>'<cr>:set hls<cr>
Bonus: CountWordFunction
fun! CountWordFunction()
try
let l:win_view = winsaveview()
let l:old_query = getreg('/')
let var = expand("<cword>")
exec "%s/" . var . "//gn"
finally
call winrestview(l:win_view)
call setreg('/', l:old_query)
endtry
endfun
" Bellow we set a command "CountWord" and a mapping to count word
" change as you like it
command! -nargs=0 CountWord :call CountWordFunction()
nnoremap <f3> :CountWord<CR>
Selecting word with mouse and counting occurrences at once: OBS: Notice that in this version we have "CountWord" command at the end
nnoremap <silent> <2-LeftMouse> :let @/='\V\<'.escape(expand('<cword>'), '\').'\>'<cr>:set hls<cr>:CountWord<cr>

- 11,069
- 3
- 50
- 40
-
-
2
-
-
-
@user5359531 what about `nnoremap
* :execute "normal! *N" – dylnmc Jun 21 '17 at 14:55` and `nnoremap # :execute "normal! #n" `? These just highlight word under cursor but don't move it to next occurence. -
Highlight all instances with double-click is a godsend. I also like to map `Ctrl-h` to get rid of highlighting: `noremap
:noh – Andy J Apr 24 '18 at 04:14`
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

- 3,778
- 1
- 20
- 19
-
2Search based solutions only move the cursor if incrememntal search is turned on. You can turn it off like this: `:set noincsearch`. In my opinion it's one of the most annoying features in Vim and one of the first things I disable for fresh installs. – jahroy Jul 11 '13 at 18:30
-
3I think `*` will unconditionally move the cursor. And I cannot see how `incsearch` might be annoying, since it's only a 'look ahead', performing current search without actually changing your position. – YvesgereY Jul 16 '13 at 10:25
-
3Wow... You're right. I assumed turning off `incsearch` would prevent the movement, but a quick test confirms you are correct. My apologies. However, it's super easy to create a mapping that prevents the movement: `:nnoremap * *N`. I can't believe you _don't_ find it annoying that your cursor jumps all over the screen as you perform a search. But... these things are completely subjective. Vive la différence ;-) – jahroy Jul 16 '13 at 18:37
-
3You can also do `:let @/='hilight_this'`, assuming you have `hlsearch` enabled. It highlights text without jumping, and you don't have to change existing settings. This changes the value that's currently stored in the search register. – Ben Davis Jun 19 '14 at 01:07
-
1This should be the accepted answer.
m does the job, no need for mapping double click. – Sassan Feb 24 '17 at 14:07 -
Can't get this to work. I installed the mark plugin, added the items to my .vimrc, but when I start vim and double click on a word, nothing happens. – user5359531 Jun 09 '17 at 17:48
-
The double-click works here but there is one side effect, everything up before the mouse click gets selected. So it needs on extra click to make the selection disapear. Anybody else noticed this side effect (and solved the issue)? – KcFnMi Jul 23 '18 at 09:11
First (or in your .vimrc):
:set hlsearch
Then position your cursor over the word you want highlighted, and hit *
.
hlsearch
means highlight all occurrences of the current search, and *
means search for the word under the cursor.

- 364,293
- 75
- 561
- 662
to highlight word without moving cursor, plop
" highlight reg. ex. in @/ register
set hlsearch
" remap `*`/`#` to search forwards/backwards (resp.)
" w/o moving cursor
nnoremap <silent> * :execute "normal! *N"<cr>
nnoremap <silent> # :execute "normal! #n"<cr>
into your vimrc.
What's nice about this is g*
and g#
will still work like "normal" *
and #
.
To set hlsearch
off, you can use "short-form" (unless you have another function that starts with "noh" in command mode): :noh
. Or you can use long version: :nohlsearch
For extreme convenience (I find myself toggling hlsearch
maybe 20 times per day), you can map something to toggle hlsearch
like so:
" search highlight toggle
nnoremap <silent> <leader>st :set hlsearch!<cr>
.:. if your <leader>
is \ (it is by default), you can press \st (quickly) in normal mode to toggle hlsearch
.
Or maybe you just want to have :noh
mapped:
" search clear
nnoremap <silent> <leader>sc :nohlsearch<cr>
The above simply runs :nohlsearch
so (unlike :set hlsearch!
) it will still highlight word next time you press * or # in normal mode.
cheers

- 3,810
- 4
- 26
- 42
-
1This should have been the accepted answer as it explains things properly. – Jay-Pi Mar 05 '21 at 19:05
-
1It should be capital `N` in both mappings, since `N` by default searches in the opposite direction. – markmarkmark May 12 '21 at 11:09
-
This definitely should have been the accepted answer. Very solid answer. Thank you! – Swivel Sep 28 '21 at 23:04
For example this plugIns:
Just search for under cursor in vimawesome.com
The key, as clagccs mentioned, is that the highlight does NOT conflict with your search: https://vim.fandom.com/wiki/Auto_highlight_current_word_when_idle
Screen-shot of how it does NOT conflict with search:
Notes:
- vim-illuminate highlights by default, in my screen-shot I switched to underline
- vim-illuminate highlights/underlines word under cursor by default, in my screen-shot I unset it
- my colorschemes are very grey-ish. Check yours to customize it too.

- 359
- 1
- 2
- 9
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

- 462
- 4
- 10
Enable search highlighting:
:set hlsearch
Then search for the word:
/word<Enter>

- 349,597
- 67
- 533
- 578
Use autocmd CursorMoved * exe printf('match IncSearch /\V\<%s\>/', escape(expand('<cword>'), '/\'))
Make sure you have IncSearch
set to something. e.g call s:Attr('IncSearch', 'reverse')
. Alternatively you can use another highlight group in its place.
This will highlight all occurrences of words under your cursor without a delay. I find that a delay slows me down when I'm wizzing through code. The highlight color will match the color of the word, so it stays consistent with your scheme.

- 1,084
- 13
- 22
Why not just: z/
That will highlight the current word under cursor and any other occurrences. And you don't have to give a separate command for each item you're searching for. Perhaps that's not available in the unholy gvim? It's in vim by default.
* is only good if you want the cursor to move to the next occurrence. When comparing two things visually you often don't want the cursor to move, and it's annoying to hit the * key every time.

- 11
-
Vim and gvim normally have the same command set. There's no reference in the help docs to a "z/" command. – wds Dec 05 '14 at 08:07
-
It's not available not only in gvim, but also in vim. Just checked it — plainly doesn't work. – Ruslan Feb 10 '15 at 11:00
-
z/ works when [this script](http://vim.wikia.com/wiki/Auto_highlight_current_word_when_idle) mentioned in [an earlier answer](http://stackoverflow.com/a/26088438/2198632) is in use. – fzzylogic Mar 19 '17 at 14:08
- Add those lines in your
~/.vimrc
file
" highlight the searched items
set hlsearch
" F8 search for word under the cursor recursively , :copen , to close -> :ccl
nnoremap <F8> :grep! "\<<cword>\>" . -r<CR>:copen 33<CR>
- Reload the settings with
:so%
In normal model go over the word.
Press * Press F8 to search recursively bellow your whole project over the word

- 5,114
- 1
- 56
- 53
--> if you want to highlight a text occurrences in gvim
Select the text & copy then ?paste the selected text (Note: This will not work for insert mode)

- 1
- 1
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 30 '23 at 12:19