1

Can tell me someone why in nvim backspace don't get triggered in insert mode when setting this keymap:

vim.api.nvim_set_keymap("i", "<bs>", "<bs><c-g>u", { noremap = true, silent = true } )

Trying to do this: https://github.com/neovim/neovim/discussions/27161

NVIM v0.9.4

Mappings:

" with autopair
:verbose imap <BS>
i <BS>  *@v:lua.MPairs.autopairs_bs()
  Last set from Lua
i <BS>  * x
  Last set from Lua
:verbose imap <Enter>
i <CR>  * <Lua 308: ~/.local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/keymap.lua:127>
          cmp.utils.set_map
  Last set from Lua

" without autopair
:verbose imap <BS>
i <BS>  * x
  Last set from Lua
[<enter> unchanged]
6
  • 1
    Welcome to Vim :-) It seems to work fine for me (If I hit Backspace Neovim start a new undo block). Maybe you could describe more precisely the symptom. What do you get? What do you expect? Commented Jan 26 at 6:37
  • Hey :) @VivianDeSmedt Okay, that's great to hear... Well, nothing happens when i press only the <bs> key. It works only in combination with other keys. Just the <bs> key on its own, works only in other modes but not in insert mode... It should delete 1 char and set a undo block... Also tried it to unmap with <NOP> but doesn't work neither... Is there a proper way to unmap the <bs> key...? Found this in the docs: nvim_del_keymap() But couldn't get this running... Docs: neovim.io/doc/user/api.html Commented Jan 28 at 15:59
  • Thanks for the feedback. As said it work for me. To test your mapping you try to remap to x to check that the basic is working (<bs> should the lead to inserting x) Then you could try <bs>y (<bs> should lead to replacing the last character by y). I'm interested by the result of the following command :verbose imap <bs> after each mappings. Commented Jan 28 at 16:51
  • 1
    I tried what you said... here is the outcome: Line 1: i <BS> *@v:lua.MPairs.autopairs_bs() Last set from Lua Line 2: i <BS> * x Last set from Lua..... The funny thing is that <bs><bs> works... heres the output: Line 1: i <BS> *@v:lua.MPairs.autopairs_bs() Last set from Lua Line 2: i <BS><BS> * <BS><C-G>U Last set from Lua.... Then i tried enter with inserting x this doesn't work nether... Output: i <CR> * <Lua 316: ~/.local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/keyma p.lua:127> cmp.utils.keymap.set_map Last set from Lua Great command by the way :D Commented Jan 28 at 19:26
  • 1
    Crazy, thank your help i could identify it... it was the cmp plugin.. Thx a lot , you saved me Commented Jan 28 at 19:44

2 Answers 2

0

From your comments it seems to me that the <BS> key is remapped at the buffer level by:

To validate my hypothesis I would deactivate these two plugins and verify that your mapping is working fine when they are deactivated.

Use the following command to verify that your mapping is in place:

:verbose imap <BS>

Or make a more obvious version (like x or <BS>y to test)

I you insist to have your mapping in the presence of the plugins you'll have to do something complex like:

  • Make sure your mapping is at buffer level and is set after the plugins make their mapping (to make sure it is not overridden)
  • To not lose the plugin functionality you'll have to add the plugin mapping (probably a <Plug> mapping) to your mapping (at the start or at the end what is the most appropriate) to get both.
0
1
" with autopair
:verbose imap <BS>
i <BS>  *@v:lua.MPairs.autopairs_bs()
  Last set from Lua
i <BS>  * x
  Last set from Lua
:verbose imap <Enter>
i <CR>  * <Lua 308: ~/.local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/keymap.lua:127>
          cmp.utils.set_map
  Last set from Lua

" without autopair
:verbose imap <BS>
i <BS>  * x
  Last set from Lua
[<enter> unchanged]

The problem was autopair: in the above text you can see the mappings on Backspace and Enter with and without the plugin.

I made a mapping with <bs> inserting x and a mapping with <enter> inserting x.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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