2

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.

1
3

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>=).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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