3

On vim, command-mode keys can be mapped through the ex command :map <key> <macro> and insert-mode keys can be mapped through :map! <key> <macro>. After mapped, the commands to remove the mapping from the command-mode keys and insert-mode keys are unmap <key> and unmap! <key> respectively.

This works well with command-mode keys, but with insert-mode keys the key expansion also works on the ex command line prompt: trying to type the key end up in the macro expansion taking place resulting in bad argument to the unmap! command (E474: Invalid Argument) or maybe the command might try to unmap some different key from the one intended (E31: No such mapping).

How can someone correctly remove a insert-mode mapped key on vim?

IanC
  • 1,968
  • 14
  • 23
  • 4
    Forget `map` and `:map!` that we used to use on plain vi decades ago. Prefer the precise mapping commands `imap`, `smap`, `xmap`, `cmap` and `nmap`. Or even better, the nore versions. With these you wouldn't have been bothered with the unwanted side effects you've experimented. – Luc Hermitte Dec 19 '16 at 19:52
  • That explains why I was having a hard time finding an answer on this issue! The book I'm reading on `vim` will probably still get to those, it starts on `vi` and then gets to `vim` (and maybe a little bit of other clones). Do they differ much from the regular `vi` mapping? I'm guessing `imap` and `cmap` stand for *insert-mode* mapping and *command-mode* mapping? – IanC Dec 19 '16 at 22:26
  • They differ in the sense that's easy to encounter undesired side effects, and in the sense that we definitively don't want them to do the same thing, which means that a simple mapping won't do. Regarding your guesses, they are correct. Just use `:help :imap` & al to see exactly what they are doing. BTW, `:help :help` is vim `man man`. – Luc Hermitte Dec 19 '16 at 23:56
  • `:cmap` is "command-line mode mapping", `:imap` is "insert mode mapping", and so on. – romainl Dec 20 '16 at 06:37

1 Answers1

3

While mapping a key, CTRL+V can be used to escape some special characters like ENTER, ESC or some particular control keys.

The same applies when using mapped insert-mode keys: they can be escaped when preceded by CTRL+V both while editing the text and while writing commands to the ex command-line prompt.

If for example, the + insert-key is mapped to some macro and the user wants to unmap it, the key must be escaped while writing the ex command to avoid the macro expansion:

:unmap! + CTRL+V (shows a ^) + <key>

The same can be done if a regular + is needed in the text while editing it in insert-mode.

IanC
  • 1,968
  • 14
  • 23