5

How to highlight all occurrences of a search without moving the cursor?

I want to highlight all occurrences of a word in the neighbourhood of the cursor without moving the cursor.

Currently, if I use * or # to highlight all occurrences of the word under the cursor, then I have to press N to get back.

3 Answers 3

7

I have run into the same problem before and spent some time to figure it out. Here is my answer. It works great.

nnoremap <Space>* <Cmd>let @/='\<'.expand('<cword>').'\>'<bar>set hlsearch<CR>

/ is a register which stores search pattern. We use let command to set it to the pattern you want to search and set hightlight to ensure it will be hightlighted.

Oh, the above only works in neovim. If you are using normal vim, it should be like this:

nnoremap <silent><Space>* :let @/='\<'.expand('<cword>').'\>'<bar>set hlsearch<CR>
6
  • How do you run the macro in vim? Pressing <Space>* works just like pressing *. What does <bar> means? :help <bar> doesn't return anything useful. Are you using register / because it's associated with the search command /, or could you use any register name?
    – Shuzheng
    Feb 15, 2020 at 16:35
  • @Shuzheng Sorry, I didn't test it on vim. It should work now after I changed <Silent> to <silent>.
    – sdp
    Feb 15, 2020 at 17:01
  • @Shuzheng <Bar> is |, which is used to run multiple commands at one time. Try :echo 123 | echo 789<CR> and see the result. See :h map_bar for the reason I use <bar> insteand of | here.
    – sdp
    Feb 15, 2020 at 17:14
  • @Shuzheng Yes, I use regitster / because it is for search pattern. Other registers have different meanings: + for system clipboard, % for the name of the current file, : for the most recent excuted command-line, and so on. (see :h registers) We can get or set the value of a register by prepending @ to it. Try :echo @%<CR>. See :h 41.3 for more information about expressions in Vim.
    – sdp
    Feb 15, 2020 at 17:26
  • 1
    @sdp Welcome to Vi and Vim! Great first answer
    – D. Ben Knoble
    Feb 15, 2020 at 19:49
3

Traditionally, one uses a mapping like

nnoremap * *N

Using matches and cursorhold has also been done

4
  • Thanks, I now use the normal mapping nnoremap <leader>* *N, and it works great! Do you also know a simple command for highlighting all words by entering the word? I would like something like nnormal <leader>/ /N, but what doesn't work.
    – Shuzheng
    Feb 15, 2020 at 16:42
  • You mean just highlighting all the matches of a search? For that you would need to use match, like in Matt’s answer
    – D. Ben Knoble
    Feb 15, 2020 at 16:58
  • But Matt expands <cword>. I want to enter the search string. What I want is / without it moving the cursor - is that possible?
    – Shuzheng
    Feb 15, 2020 at 17:34
  • You could probably do it with a custom command, sure
    – D. Ben Knoble
    Feb 15, 2020 at 17:54
1

Maybe like this:

nnoremap <leader>h :execute 'match Search /\V'..escape(expand('<cword>'), '\/')..'/'<CR>

But note that you'll need :match none to switch it off.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.