vim Configuring Vim Mappings


Example

  • Don't put comments after mappings, it will break things.
  • Use :map <F6> to see what is mapped to <F6> and in which mode.
  • Use :verbose map <F6> to also see where it was last mapped.
  • :map and :map! are too generic. Use :n[nore]map for normal mode mappings, :i[nore]map for insert mode, :x[nore]map for visual mode, etc.

Recursive mappings

Use recursive mappings only if you intend to use other mappings in your mappings:

nnoremap b     B
nmap     <key> db

In this example, b is made to work like B in normal mode. Since we use b in a recursive mapping, pressing <key> will work like dB, not like db.

Non-recursive mappings

Use non-recursive mappings only if you intend to use default commands in your mappings, which is almost always what you want:

nnoremap <key> db

In this example, we use b in a non-recursive mapping so pressing key will always work like db, whether we remapped b or not.

Executing a command from a mapping

nnoremap <key> :MyCommand<CR>

Executing multiple commands from a mapping

nnoremap <key> :MyCommand <bar> MyOtherCommand <bar> SomeCommand<CR>

Calling a function from a mapping

nnoremap <key> :call SomeFunction()<CR>

Mapping a <Plug>mapping

map <key> <Plug>name_of_mapping

See :help map-commands, :help key-notation and :help <plug>.

see Key Mappings in Vim for futher read