vim Basic mapping


Example

map Overview

A key sequence can be re-mapped to another key sequence using one of the map variants.

As an example, the following typical map will exit Insert mode when you press jk in quick sequence:

:inoremap jk <Esc>

map Operator

There are multiple variants of :map for different modes.

CommandsModes
:map, :noremap, :unmapNormal, Visual and Operator-pending mode
:map!, :noremap!, :unmap!Insert and Command-line mode
:nmap, :nnoremap, :nunmapNormal mode
:imap, :inoremap, :iunmapInsert and Replace mode
:vmap, :vnoremap, :vunmapVisual and Select mode
:xmap, :xnoremap, :xunmapVisual mode
:smap, :snoremap, :sunmapSelect mode
:cmap, :cnoremap, :cunmapCommand-line mode
:omap, :onoremap, :ounmapOperator pending mode

Usually, you should use the :noremap variants; it makes the mapping immune to remapping and recursion.

map Command

  • You can display all mappings using :map (or one of the variations above).
  • To display the current mapping for a specific key sequence, use :map <key> where <key> is a sequence of keys
  • Specials keys like Esc are mapped using special <> notation, like <Esc>. For the full list of key codes, see http://vimdoc.sourceforge.net/htmldoc/intro.html#keycodes
  • :nmapclear - Clear all normal mode maps
  • :nunmap - Unmap a normal mode map
  • You can configure the maximum time between keys of a sequence by changing the timeout and ttimeout variables

Examples

  • imap jk <Esc>: typing jk in insert mode will bring you back to normal mode
  • nnoremap tt :tabnew<CR>: typing tt in normal mode will open a new tab page
  • nnoremap <C-j> <C-w>j: typing <C-j> in normal mode will make you jump to the window below and to the left
  • vmap <C-c> \cc: typing <C-c> in visual mode will execute \cc (NERDCommenter command to comment the line). As this relies on a plugin mapping, you cannot use :vnoremap here!

futher reading here