(Vim) Set custom key mappings
- Use
nnoremapto define macros that will be executed in command mode- Syntax:
nnoremap <KEY_ACTION> <ACTION_TO_TAKE> - E.g.: Toggle NERDTree sidebar using
\\ + f:nnoremap <Leader>f :NERDTreeToggle<Enter> - (Important)
<Leader>: A “mapleader” variable, useful for defining custom mappings without collision with existing mappings (typically set to\\) - There are also different variants such as
cnoremap,vnoremap, etc.; refer to:help nnoremapfor details
- Syntax:
- Symbol notations for non-letter keys provided by Vim
- Notation provided to enhance readability of mapping definitions
- E.g.:
<Space>,<Esc>,<Del> <CR>: Carriage Return (=<Enter>,<Return>)- Run
:help key-notationto see a full list of such symbols
- Mappings predefined by Vim
- E.g.
<C-r><C-w>inserts the word currently under the cursor- E.g. Start off a global substitution command for exact matches of the word under cursor:
nnoremap <Leader>s :%s/\<<C-r><C-w>\>/
- E.g. Start off a global substitution command for exact matches of the word under cursor:
c_prefix means that the mapping works in command mode- Refer to
:help cmdlinefor more details
- E.g.
- Use
:help <ITEM_TO_SEARCH>to search for occurrence of<ITEM_TO_SEARCH>in Vim manual, e.g.:help <Leader>,:help <C-r><ITEM_TO_SEARCH>doesn’t have to be a complete (key)word; lookup based on a part of (key)word is also supported
References:
Written on March 9, 2021
