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>
There are multiple variants of :map for different modes.
| Commands | Modes |
|---|---|
:map, :noremap, :unmap | Normal, Visual and Operator-pending mode |
:map!, :noremap!, :unmap! | Insert and Command-line mode |
:nmap, :nnoremap, :nunmap | Normal mode |
:imap, :inoremap, :iunmap | Insert and Replace mode |
:vmap, :vnoremap, :vunmap | Visual and Select mode |
:xmap, :xnoremap, :xunmap | Visual mode |
:smap, :snoremap, :sunmap | Select mode |
:cmap, :cnoremap, :cunmap | Command-line mode |
:omap, :onoremap, :ounmap | Operator pending mode |
Usually, you should use the :noremap variants; it makes the mapping immune to remapping and recursion.
:map (or one of the variations above).:map <key> where <key> is a sequence of keys<> 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 maptimeout and ttimeout variablesimap jk <Esc>: typing jk in insert mode will bring you back to normal modennoremap tt :tabnew<CR>: typing tt in normal mode will open a new tab pagennoremap <C-j> <C-w>j: typing <C-j> in normal mode will make you jump to the window below and to the leftvmap <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
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:
:nnoremap <Leader>a somecomplexaction
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.
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 <c-s> :w<CR>
inoremap <c-s> <c-o>:w<CR>
The 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).
Similarly,
" This is commented, as Ctrl+Z is used in terminal emulators to suspend the ongoing program/process.
" nnoremap <c-z> :u<CR>
" Thus, Ctrl+Z can be used in Insert mode
inoremap <c-z> <c-o>:u<CR>
" Enable Ctrl+C for copying selected text in Visual mode
vnoremap <c-c> <c-o>:y<CR>
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.