How to remap search selection (neovim 0.8)

In neovim 0.8, improved search selection is implemented (#18538):

  • *, #

I remaped those keymaps as followings:

vim.keymap.set("v", ";", "*", {noremap=true))
vim.keymap.set("v", "+", "#", {noremap=true))

After that,

  • Select a word in visual mode
  • Hit “;”

As result, the action is not same as hitting “*”. Selected range is expanded and escaping visual mode is needed.

DId I make any mistakes to remap that?

Here is what I found. Run verbose map * and you’ll see:

 x  *           * y/\V<C-R>"<CR>
                 Nvim builtin
        Last set from Lua
Press ENTER or type command to continue

According to the docs (:map-listing), the * before the rhs means that it is not remappable. I’m not entirely sure if this explains why your mapping doesn’t work.

But here is how you can make it work. Use that exact same definition in your mapping:

vim.keymap.set("x", ";", 'y/\\V<C-R>"<CR>', {noremap=true})

Similarly for the other mapping:

vim.keymap.set("x", "+", 'y?\\V<C-R>"<CR>', {noremap=true})

(Note that there is a bracket mismatching typo in the mappings you posted).

Thank you for your perfect answer and I apologize for being too late to respond to your reply.

It fixed my issue completely!

1 Like