Key Mappings in Vim
Updating Vim key mappings allows you to solve two kinds of problems: Re-assigning key commands to letters that are more memorable or accessible, and creating key commands for functions which have none. Here you will learn about the various ways to [re]map key commands, and the context to which they apply (i.e. vim modes)
A key sequence can be re-mapped to another key sequence using one of the
As an example, the following typical
map will exit Insert mode when you press jk in quick sequence:
There are multiple variants of
:map for different modes.
|Normal, Visual and Operator-pending mode|
|Insert and Command-line mode|
|Insert and Replace mode|
|Visual and Select mode|
|Operator pending mode|
Usually, you should use the
:noremap variants; it makes the mapping immune to remapping and recursion.
- You can display all mappings using
:map(or one of the variations above).
- To display the current mapping for a specific key sequence, use
<key> is a sequence of keys
- Specials keys like Esc are mapped using special
<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
imap jk <Esc>: typing
jkin insert mode will bring you back to normal mode
nnoremap tt :tabnew<CR>: typing
ttin 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
futher reading here
Illustration of Basic mapping (Handy shortcuts).
In most text editors, the standard shortcut for saving the current document is Ctrl+S (or Cmd+S on macOS).
Vim doesn't have this feature by default but this can be mapped to make things easier. Adding the following lines in
.vimrc file will do the job.
nnoremap command maps Ctrl+s to
:w (write current contents to file) command whereas the
inoremap command maps the Ctrl+S to
:w command and returns back to the insert mode (
<c-o> goes into normal mode for one command and returns to insert mode afterwards, without altering cursor position which other solutions like
<esc>:w<cr>a cannot ensure).
PS: However it must be noted that Ctrl+S may not work as expected while using ssh (or PuTTY). The solution to this is not within the scope of this document, but can be found Here.
Map leader key combination
The leader key could be used as a way to create a mapping with a key-binding that can be overridden by the end user.
The leader is the \ key by default. In order to override it, the end-user would have to execute
:let g:mapleader='somekey(s)' before defining the mapping.
In a typical scenario, the mapleader is set in the
.vimrc, and plugins use
<Leader> in the keybinding part of their mappings to have them customizable.
In the plugin, we would define mappings with:
This would map the somecomplexaction action to the \+a key combination.
The a action without the leader does not change.
It's also possible to use
<Plug>Mappings to leave more room to customise plugins keybindings.