Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Why does noremap not work in insert mode?

Tags:

vim

keyboard

Consider the unbinding of the arrow keys using

 noremap <Left>  <NOP>
 noremap <Right> <NOP>
 noremap <Up>    <NOP>
 noremap <Down>  <NOP>

This works in normal mode, but it does not work in insert mode: one can still navigate with the arrow keys. As a countermeasure, one must include

 inoremap <Left>  <NOP>
 inoremap <Right> <NOP>
 inoremap <Up>    <NOP>
 inoremap <Down>  <NOP>

But this doesn't really make sense to me, since I assume map and noremap should work in all modes, while prepending n/v/x/s/o/i/l/c specifies the mapping to work only within that specific mode. Is there a reason for this?

like image 417
Dustin Tran Avatar asked Oct 18 '12 11:10

Dustin Tran


2 Answers

why there isn't an all-inclusive modal map, rather than issuing both map and map!

That's easy to explain: In insert mode mappings, Vim doesn't automatically switch to normal mode (you may want to stay in insert mode, though text translations are typically done via :iabb, not via :imap), so the set of applicable commands is totally different. For example, in normal mode Ctrl-U scrolls upwards, but in insert mode it deletes the entered characters in the line!

Prefixes like <C-O> temporarily switch from insert mode to normal mode. Actually, one often even has to define a different prefix for command line mode, too, as shown by this example:

noremap <C-Tab> :<C-U>tabnext<CR>
inoremap <C-Tab> <C-O>:tabnext<CR>
cnoremap <C-Tab> <C-C>:tabnext<CR>

So when defining mappings, always consider in which modes they are needed and whether they need remapping (:nmap vs. :noremap, prefer the latter).

like image 132
Ingo Karkat Avatar answered Sep 19 '22 12:09

Ingo Karkat


:help map-overview

map (and noremap) are for normal, visual, select and operator-pending modes.

like image 43
Paul Ruane Avatar answered Sep 22 '22 12:09

Paul Ruane