How To Wiki

How to map keys in vim

Redirected from Howto map keys in vim

1,795pages on
this wiki
Add New Page
Add New Page Talk0

Say you want to setup vim to have a certain key stroke, preform a desired operation You can use the map command to do this.

The file[s] to add the mappings are ~/.vimrc or /etc/vim/vimrc or you can add a map, temporarily, through vim command-line.

Basic mappingEdit

  • Add to config file
    • Example: map <F5> dd
  • Shift, Ctrl, Alt, Meta, Shift: <S-...>
    • Shift: <S-...>
      • Example: Shift s
    • Ctrl: <C-...>
      • Example: Ctrl s
    • Alt and Meta are the same: <A-...> <M-...>
      • Example: Alt s
        <A-s> or <M-s>

Special Characters

  • Basic
    • <F1> through <F12>
    • Enter: <CR> or <Enter> or <Return>
    • Space bar: <Space>
    • Esc key: <Esc>

Mapping in different modesEdit

Keys can be mapped in all types of modes, all mapping syntaxes are the same as described above

  • Modes in vim
    • Normal mode: When typing commands.
    • Visual mode: When typing commands while the Visual area is highlighted.
    • Operator-pending mode: When an operator is pending (after "d", "y", "c", etc.). Example: ":omap { w" makes "y{" work like "yw" and "d{" like "dw".
    • Insert mode. These are also used in Replace mode.
    • Command-line mode: When entering a ":" or "/" command.
    • Lang-Arg: searching
  • mapping commands and their respective modes
    • map
      • Normal, Visual, Operator-pending
    • map!
      • Insert, Command-line
    • nmap
      • Normal
    • vmap
      • Visual
    • omap
      • Operator-pending
    • cmap
      • Command-line
    • imap
      • Insert
    • lmap
      • Insert, Command-line, Lang-Arg

Prevent recursive mapping (noremap)Edit

You can prevent one mapping from using other map by using noremap. This is called recursion in programming

Example of a recursive mapping

  • You want the following
    • map <F5> c<Space>
    • map <Space> <PageDown>
  • But you don't want
    • map <F5>, c<Space> to turn into map <F5> c<PageDown>

To prevent this

  • Change
    • map <F5> c<Space>
    • noremap <F5> c<Space>

This works with all mapping types

  • nnoremap
  • inoremap
  • vnoremap
  • etc.

External linksEdit

From HowTo Wiki, a Wikia wiki.