Vim: search and highlight but do not jump

I haven't seen this one yet:

nmap <silent> * "syiw<Esc>: let @/ = @s<CR>

It's very short and does not involve jumping around which can result in blinking.

Explanation: copy the word under cursor to s register and then set the search register (/) to the content of s register. The search register is not writeable directly, that's why the let is necessary and hence the silent to keep vim's command line clean.


I found this works pretty well, there's no blink and it doesn't need an intermediate register.

nnoremap <silent> * :let @/= '\<' . expand('<cword>') . '\>' <bar> set hls <cr>

Or if you want the g* behavior:

nnoremap <silent> g* :let @/=expand('<cword>') <bar> set hls <cr>

The best solution:

  1. don't add a jump to the jump list
  2. the behavior of the star key is not be changed

so, try the plugin: http://www.vim.org/scripts/script.php?script_id=4335

Much better than:

" a jump adds to the jump list
nnoremap * *``
" I got a dead loop on macvim
nnoremap * :keepjumps normal *``<cr>
" the behavior is changed
nnoremap <silent> <Leader>* :let @/='\<<C-R>=expand("<cword>")<CR>\>'<CR>:set hls<CR>

I would map:

nnoremap * *``

Works exactly like you want, except that it adds a jump in the jump list. To prevent that you need:

nnoremap * :keepjumps normal! mi*`i<CR>

Tags:

Vim

Search