5

Notepad++ has this really neat feature that if you select one word, every other occurence of the word is automatically highlighted, like this (only the first autocmd is highlighted by me):

N++ highlighting screenshot

I am aware that a similar thing has already been asked, but I am hoping that there is a solution which does not require manual interaction.

Community
  • 1
  • 1
Nicolas
  • 1,828
  • 6
  • 23
  • 34
  • 1
    What do you mean by a manual interaction? Do you want vim to highlight matches always? – pkacprzak Jul 11 '13 at 17:51
  • I agree it's unclear what the OP wants. Are you selecting words with your mouse? – jahroy Jul 11 '13 at 22:16
  • sorry for being unclear. by selecting i mean highlighting one word. if the cursor is at the beginning of the word, shift+ctrl (alt on mac)+ right arrow. if i do this with the first word, all other occurences immediately are highlighted as well in a different color. – Nicolas Jul 11 '13 at 22:39

4 Answers4

18

If you press * Vim will search for all occurrences of the word under the cursor.

If you have search highlighting turned on, they will be highlighted.

You can turn on search highlighting by putting this in your .vimrc:

set hlsearch

One nice plus is you don't have to select the word anymore... you just hit the * key.

(you could map it to another key to make it even more convenient)


If you don't like the way the cursor moves when you press * you can prevent it with a simple mapping:

nnoremap * *N

(put it in your .vimrc)

This keeps the cursor in its current position when you press *.

jahroy
  • 22,322
  • 9
  • 59
  • 108
  • selecting would be highlighting a word in visual mode. when the cursor is at the first letter, v e would be the vim keys. * in combination with hlsearch is nice, only that it moves the cursor. is it possible to do it without changing the cursor position? – Nicolas Jul 11 '13 at 22:42
  • ... check out the second half of my answer. It suggests a mapping that disables the cursor movement. – jahroy Aug 09 '13 at 04:32
2

Something like this could work.

set updatetime=10

autocmd! CursorHold,CursorHoldI * let @/='\<'.expand('<cword>').'\>'

This uses CursorHold events to set the / register the word under the cursor. To make the CursorHold events fire more often updatetime was lowered to 10 (from 4000).

Problems:

  1. You need to :set hlsearch manually. For some reason it doesn't turn on by itself.
  2. This interferes with normal searching. Since you are changing the / register when ever the cursor moves.

Overall I would not to use this since I think searching is more important than this feature.

FDinoff
  • 30,689
  • 5
  • 75
  • 96
  • 3
    You could probably use `:match` instead of stomping on the search register, but the approach would be the same. This is probably the best way of accomplishing what the OP wants. – Randy Morris Jul 11 '13 at 18:25
2

My SearchHighlighting plugin has a <Leader>* mapping and :SearchAutoHighlighting command to enable this. It can highlight the current word / WORD / whole word / line / exact line under the cursor.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
1

I would do it like this:

:au CursorHold,CursorHoldI * :try | exe "call matchdelete(w:my_match)" | catch |  finally | let w:my_match = matchadd('IncSearch', expand('<cword>')) | endtry

If you don't like the highlighting, select a nice one from the output of :hi and use it instead of IncSearch. Note: This might slow down Vim considerably.

Christian Brabandt
  • 8,038
  • 1
  • 28
  • 32