Is there a way to prevent cursor to search forward when using the *
or /
?
I just want to highlight the word I am on & others, but not move to forward nor backward.
This will do for the *
search, I'm not sure how to do that for /
.
nnoremap <silent> * :let @/= '\<' . expand('<cword>') . '\>' <bar> set hls <cr>
@/
is the search register, which we set to <cword>
, the word under the cursor, surrounded by words boundaries \<
\>
to match actual words rather than parts of words.
g*
matches even "non words". We can adapt it as well :
nnoremap <silent> g* :let @/=expand('<cword>') <bar> set hls <cr>
For visual mode:
vnoremap * y/\V<C-R>=escape(@",'/\')<CR><CR>
This copies the selection into the unnamed register @"
(:h registers
), starts a search with its content, backslashes escaped, via the :h expression register
(inserted with <C-R>=
).