Search code examples
vimeditorkey-bindings

In vim, is there a way to rebind "*" to highlight the current word, without advancing to the next?


I often use * to highlight all instances of the current word, and the fact that it advances to the next word is pretty annoying. I'd like to disable this behavior, knowing that I can always use "n" if I actually need to advance.

Any insight?

EDIT: I should add that I'd like to avoid a screen redraw at all costs as it is visually distracting.


Solution

  • Try this:

    nnoremap * :let @/ = "\\<<C-R><C-W>\\>"<CR>
    

    (Assumes you have 'hlsearch' on). This just changes the current search pattern to the word under the cursor (surrounded by \< and \> to match word boundaries). If you have hlsearch enabled, it will highlight the word. n and N will then work as normal.

    See:

    :help :let-@
    :help quote/
    :help c_CTRL-R_CTRL-W