0

(I'm using Alacritty + NeoVim, I'm not sure about whether this issue is from Alacritty or NeoVim.)

I want to map <BS> in insert mode to do nothing, i.e. <Nop>. I have tried:

inoremap <BS> <Nop>
noremap! <BS> <Nop>

I have checked that by typing Controlv then press , the output is exactly <BS> in insert mode.

But the backspace indeed disabled in the mode when I press :. So how to do the same for insert mode?

4
  • 1
    The inoremap <BS> <Nop> command worked both in Vim and NeoVim for me. Commented Aug 16, 2021 at 0:00
  • Use this command: verbose imap <BS> to report what, if anything, <BS> is mapped to. Commented Aug 16, 2021 at 0:01
  • 1
    @ChrisHeithoff: Sir you are awesome, I read that line in the file that is exactly what cause this behaviour. Commented Aug 16, 2021 at 5:20
  • Is it possible to detect the current input-method chosen in (Neo)Vim? I only want to stop <BS> when certain input-method is chosen. I need the auto-pairs plugin, lol. Commented Aug 16, 2021 at 5:20

1 Answer 1

1

Since I have solved this myself with the help of comments by @Chris Heithoff I'm going to answer it myself. As comment stated: when some mapping should work but it didn't, thne the reason is probably because some plugins also doing the mapping for the same key. To check it:

:verbose imap YOUR_KEY

where YOUR_KEY is optional, if omitted then all will be listed. And I found this is useful since I want to read them in a larger window.


If you're interested about the intent of <BS> to <Nop> mapping:

In short:

  • Alacritty doesn't support inline editing when typing Chinese in insert mode.
    • For example, there are 3 components ㄋㄧˇ to form the word , Both these components and the result will not be shown in insert mode before I hit Enter after typing them all.
  • Even worse, if I want to delete some component before I hit the Enter, it will delete not only the last component BUT ALSO the word already entered before . (I hate it!)

So what I wanted to do is a shortcut to toggle <BS> when entering insert mode. Here is the result :)

" Chinese Input.
inoremap <Up> <Nop>
inoremap <Down> <Nop>
inoremap <Left> <Nop>
inoremap <Right> <Nop>
inoremap <nowait> <C-C> <Esc>:call <SID>toggleLockOnBS()<CR>a
inoremap <nowait> <Esc> <Esc>:call <SID>removeBSLock()<CR>
let s:lockOnBS=0
function! s:toggleLockOnBS() abort
    if s:lockOnBS
        call s:removeBSLock()
    else
        call s:putBSLock()
    endif
endfunction
function! s:removeBSLock() abort
    execute 'inoremap <buffer><silent> <BS> <C-R>=AutoPairsDelete()<CR>'
    echo '<BS> enabled for 中文.'
    let s:lockOnBS = 0
endfunction
function! s:putBSLock() abort
    execute 'inoremap <buffer><silent> <BS> <Nop>'
    echo '<BS> disabled for 中文.'
    let s:lockOnBS = 1
endfunction

the line inoremap <buffer><silent> <BS> <C-R>=AutoPairsDelete()<CR> is to restore the mapping of some plugin which also want to map <BS> to something else. Feel free to change it to whatever you like.

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.