Search code examples
searchvim

Vim: search and highlight but do not jump


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. If hlsearch is enabled, it will also highlight the matches.

I want to be able to press * and get the highlighted matches and be able to navigate the matches using the n key. However, I do not want Vim to jump to the next match when * is pressed, it should remain on the current word. Is there a way to do this?


Solution

  • 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>