user3442536
user3442536

Reputation: 45

Adding to mapping rather than completely remapping

I'm working on a plugin to allow bracket completion (I know it's available, it's more of a learning exercise). To properly implement it, I need to add to the backspace mapping. However, as it's an important key, I'd rather keep the existing functionality and just add to it rather than reimplementing the functionality. The steps would basically be when in insert mode and press backspace, execute the original backspace key, then check for some conditions and maybe remove more characters.

I've tried something like imap <backspace> <backspace><call_func_here>, but that doesn't seem to work. Again, I know I could remap backspace to just the function and try to recreate the backspace functionality, but I'd prefer to not do that.

Is this possible in vim?

Upvotes: 0

Views: 94

Answers (1)

statox
statox

Reputation: 2886

I think what you are trying to do is the following:

inoremap <silent> <BS> <BS><C-o>:call MyFunction()<CR>
  • inoremap allows to create a non recurrent mapping in insert mode (it is often a good idea to use nore in your mappings). :h :inoremap
  • <silent> precise that the mapping will not be echoed on the command line (You will not see :call MyFunction() in the command line) :h :map-silent
  • <BS> is the reference to the backspace key that you want to remap.
  • The second <BS> is here to issue a backspace in insert mode
  • <C-o> switches to normal mode for only a command. :h i_CTRL-O
  • :call MyFunction() is the call to your function the way you would do it in normal mode.
  • <CR> correspond to the Enter key which validate the call to your function.

Upvotes: 2

Related Questions