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 mapping[]
- Add to config file
- map KEY_SEQUENCE OPERATION
- Example: map <F5> dd
- Shift, Ctrl, Alt, Meta, Shift: <S-...>
- Shift: <S-...>
- Example: Shift s
- <S-s>
- Example: Shift s
- Ctrl: <C-...>
- Example: Ctrl s
- <C-s>
- Example: Ctrl s
- Alt and Meta are the same: <A-...> <M-...>
- Example: Alt s
- <A-s> or <M-s>
- Example: Alt s
- Shift: <S-...>
Special Characters
- Basic
- <F1> through <F12>
- Enter: <CR> or <Enter> or <Return>
- Space bar: <Space>
- Esc key: <Esc>
- All Special keys
Mapping in different modes[]
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
- map
Prevent recursive mapping (noremap)[]
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>
- and
- map <Space> <PageDown>
- map <F5> c<Space>
- But you don't want
- map <F5>, c<Space> to turn into map <F5> c<PageDown>
To prevent this
- Change
- map <F5> c<Space>
- to
- noremap <F5> c<Space>
- map <F5> c<Space>
This works with all mapping types
- nnoremap
- inoremap
- vnoremap
- etc.
External links[]
From HowTo Wiki, a Wikia wiki.