The super star (*) key in Vim will search for the word under the cursor and jump forward to the next match. The user can jump to the next matches with the n key.
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>
My solution:
nnoremap <silent><expr> * v:count ? '*'
\ : ':execute "keepjumps normal! *" <Bar> call winrestview(' . string(winsaveview()) . ')<CR>'
nnoremap <silent><expr> g* v:count ? 'g*'
\ : ':execute "keepjumps normal! g*" <Bar> call winrestview(' . string(winsaveview()) . ')<CR>'
Pros:
*
.*
, its behavior is almost identical with *
(except for jumping).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>
If you want to keep the current view and add the search to the history, try this [not so efficient] solution:
noremap * msHmt`s*`tzt`s
It is using the marks s
(save) and t
(top).
A simple solution came to my mind: put map * *#
in .vimrc
file (it will blink though).
Many answers here outline rather simple mappings that work well for common cases, but may have side effects (like flickering by jumping back and forth) or lack robustness (may break if some regexp characters are defined as keyword characters).
If you're looking for a robust implementation and don't mind installing a plugin, you can choose from a plethora of alternatives, many of which also offer additional search-related improvements:
*
command, extends it to visual selections, and offers optional auto-searching of the word under the cursor.*
to not jump to the next match, and includes the visual search from the next pluginn/N
or *
in the visual selection, and can avoid jumping.z*
mapping that also doesn't jump, visual *
, more intuitive smartcase handling, and can keep the cursor position when jumping (like ,*
)