Search code examples
vim

Vim key mappings in insert mode


I have edited my .vimrc file and mapped some commands. They are only working in normal mode. Is there any way to map commands in insert mode? (e.g. commands involved with special keys such as Ctrl) For example, can I copy in insert mode using Ctrl+c?


Solution

  • The first letter in the :map commands determines which modes (:h map-modes) they apply to. So :nnoremap is for normal mode, and :inoremap for insert mode.

    You usually cant' just use the same right-hand side; you need to consider that you're in a different mode. To invoke a (normal mode) command from insert mode:

    • prepend <Esc> if you want to stay in normal mode after the mapping
    • prepend <C-o> if you want to continue in insert mode after the mapping; this command switches to normal mode for just one command

    For example, to map :w to <C-s>, you'd use this: :nnoremap <C-s> :w<CR>. The corresponding insert mode mapping (staying there) is:

    :inoremap <C-s> <C-o>:w<CR>