vimtricks.wiki Concise Vim tricks, one at a time.

How do I create a custom key mapping?

Answer

:nnoremap key command

Explanation

The :nnoremap command creates a non-recursive normal mode mapping. It binds a key sequence to a command without allowing the mapping to trigger other mappings.

How it works

  • :nnoremap creates a normal mode mapping (non-recursive)
  • :inoremap creates an insert mode mapping
  • :vnoremap creates a visual mode mapping
  • noremap prevents the right side from triggering other maps

Example

nnoremap <leader>s :w<CR>          " Quick save
nnoremap <C-n> :bnext<CR>          " Next buffer
nnoremap <leader>/ :nohlsearch<CR> " Clear search
inoremap jk <Esc>                  " Exit insert mode

Tips

  • Always use noremap variants to avoid recursive mapping issues
  • :nmap shows existing normal mode mappings
  • :nunmap key removes a mapping
  • <silent> suppresses command echo: nnoremap <silent> ...
  • Test mappings with :nnoremap key before adding to vimrc

Next

How do you yank a single word into a named register?