Vim: automatically selects all occurrences of the selected word

Notepad ++ has this really neat feature: if you select one word, every other word event will be automatically highlighted like this (only the first autostart is highlighted by me):

N ++ highlighting screenshot

I know that a similar thing has already been set , but I hope that there is a solution that does not require manual interaction.

+4
source share
4 answers

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

+2
source

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

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

You can turn on search highlighting by placing it in your .vimrc :

 set hlsearch 

One nice plus is that you no longer have to choose a word ... you just press the * key.

(you can match it with 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 match:

 nnoremap * *N 

(put it in your .vimrc)

This holds the cursor in the current position when you press * .

+11
source

Something like this might work.

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

It uses CursorHold events to set / register a word under the cursor. In order for CursorHold events to fire more often, the time update was reduced to 10 (from 4000).

Problems:

  • You need to :set hlsearch manually. For some reason, it does not turn on by itself.
  • This interferes with the normal search. Since you change the case / when the cursor moves.

In general, I would not use this, because I believe that search is more important than this function.

+1
source

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 highlight, select a good result from the output :hi and use it instead of IncSearch. Note. This can significantly slow down Vim.

+1
source

Source: https://habr.com/ru/post/1490965/


All Articles