173

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

ivanleoncz
  • 5,662
  • 3
  • 49
  • 42
Matteo Cocon
  • 1,831
  • 2
  • 12
  • 6

15 Answers15

219

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.

wilhelmtell
  • 53,297
  • 19
  • 89
  • 128
  • 38
    If 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
  • 1
    Also, be aware that the cursor will move to the previous (#) or next (*) occurrence of the word – Juan Campa Jul 30 '13 at 21:40
  • 1
    But you can go back to the position where you pressed `*` or `#` by `o` in normal mode. – Ruslan Feb 10 '15 at 10:57
  • 10
    Instead of `:nohlsearch`, you can just use `:noh` – crisron Nov 29 '15 at 10:18
  • 2
    Set in `.vimrc`: `noremap :set hlsearch! hlsearch?` for toggle between highlight and remove highlight by ``. – Chalist Jun 20 '17 at 02:12
132

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

sleepynate
  • 7,296
  • 2
  • 24
  • 38
  • 34
    Only if `&hlsearch` is on. – wilhelmtell Aug 07 '10 at 16:41
  • 10
    It will also jump to the next occurrence of the word under the cursor. If you want to jump to the previous occurrence, use `#`. – Vinicius Pinto Sep 22 '14 at 22:52
  • 9
    Can I make it just stay on the current position? – nn0p Dec 13 '16 at 17:53
  • @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
  • 1
    is there a way to use this functionality but with the highlighted one or more words? – Sung Cho 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 #` would search forward with `Ctrl`+`n` and backwards with `Ctrl`+`p`. – steffen Apr 17 '21 at 11:54
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
clagccs
  • 2,130
  • 2
  • 20
  • 32
  • 1
    Is 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
19

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>
SergioAraujo
  • 8,735
  • 1
  • 46
  • 37
10

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
YvesgereY
  • 3,223
  • 1
  • 16
  • 18
  • 2
    Search 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
  • 3
    I 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
  • 3
    Wow... 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
  • 3
    You 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
  • 1
    This 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
4

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.

Ned Batchelder
  • 323,515
  • 67
  • 518
  • 625
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
kashif
  • 454
  • 4
  • 9
3

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

dylnmc
  • 3,276
  • 3
  • 20
  • 37
3

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: enter image description here 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.
Xopi García
  • 111
  • 1
  • 7
2

Enable search highlighting:

:set hlsearch

Then search for the word:

/word<Enter>
John Kugelman
  • 307,513
  • 65
  • 473
  • 519
2

My favorite for doing this is the mark.vim plugin. It allows to highlight several words in different colors simultaneously.

Example Screenshot

Habi
  • 3,095
  • 21
  • 21
1

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.

Seph
  • 976
  • 11
  • 20
1
set hlsearch

maybe ?

Rook
  • 54,867
  • 44
  • 156
  • 233
0

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.

INL
  • 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
0
  1. 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>

  1. Reload the settings with :so%
  2. In normal model go over the word.

  3. Press * Press F8 to search recursively bellow your whole project over the word

Yordan Georgiev
  • 4,006
  • 1
  • 43
  • 47