Mappings & Key Bindings
No two programmers work the same way. Some save obsessively, others navigate by search, others by marks. Vim embraces this diversity through mappings — custom key bindings that let you reshape the editor to match your habits, not the other way around. Understanding Vim's modes (see Modes & Navigation Basics) is essential here, since each mapping targets a specific mode.
This chapter shows you how to create mappings that will become second nature. We'll start with the fundamentals, work through essential mappings that most Vim users swear by, and finish with Neovim's Lua-based approach. By the end, you'll have the tools to make any keystroke do exactly what you need.
What you'll learn in this chapter:
Create non-recursive mappings for Normal, Insert, Visual, and Command-line modes
Configure and use the leader key as a namespace for personal key bindings
Build practical mappings for saving, buffer navigation, line movement, and spell checking
Define custom text objects and operator-pending mappings
Write mappings in Neovim's Lua API with
vim.keymap.set
Mapping Basics
The general form is:
{mode}map {lhs} {rhs}Mapping Modes
nmap / nnoremap
Normal mode
nnoremap <leader>w :w<CR>
imap / inoremap
Insert mode
inoremap jk <Esc>
vmap / vnoremap
Visual mode
vnoremap < <gv
cmap / cnoremap
Command-line mode
cnoremap w!! w !sudo tee %
omap / onoremap
Operator-pending mode
onoremap il :norm vil<CR>
Tip: Always use
noremapvariants (e.g.,nnoremapinstead ofnmap) to avoid recursive mapping issues.
The Leader Key
The leader key is a configurable prefix for your personal mappings:
Tip: Space is the most popular leader key because it's easy to press and doesn't conflict with existing mappings.
Essential Mappings
Quick Save
Tip: Prefer
:updateover:w— it only writes if the file was actually changed.
Toggle Options
Toggle Search Highlighting
A more comprehensive highlight toggle that also clears manual matches:
This clears both :hlsearch highlighting and any matches added via matchadd() or matchaddpos().
Buffer Navigation
Quickfix Navigation
cgn Mappings
Start search-and-replace from the current word:
Joining Lines Without Moving Cursor
Using the Preserve function:
Jump to Last Change
Save as Root
Line Text Objects
Vim doesn't have built-in line text objects, but we can create them:
Now you can:
yil— copy line content without newline (characterwise)dil— erase line content without removing the linedal— copy/delete the full line including leading space
Spell Checking Mappings
The <C-g>u inserts an undo-break, so you can undo the correction separately.
Spell commands:
]s/[s— jump to next/previous misspelled wordz=— show suggestionszg— add word to dictionaryzw— mark word as incorrect
Better Visual Mode Indenting
By default, indenting in Visual mode deselects the text. These mappings keep the selection:
Now you can press > multiple times to keep indenting without re-selecting.
Move Lines Up/Down
Move selected lines or the current line up and down:
Re-indent Entire File
Using the Preserve function to re-indent without moving the cursor:
Opening Config File
Tip:
:drop fileopens the file in a window. If already open, it switches to that window.
Debugging Mappings
Check If a Key is Already Mapped
Vim shows the mapping and which file defined it.

List All Mappings
Neovim Lua Mappings
In Neovim with Lua, use vim.keymap.set:
Tip: The
descfield is important — it shows up in plugins like which-key.nvim, which displays available keybindings as you type. See Chapter 11: Neovim with Lua.
Note: For a comprehensive guide to organizing your mappings and other settings in configuration files, see Configuring Vim & Neovim.
Summary
Mappings are the primary way to customize Vim's behavior and build a personalized editing environment. By binding key sequences to commands in specific modes, you can create shortcuts for frequent operations, build custom text objects, and streamline your entire workflow. Whether you use Vimscript's nnoremap or Neovim's vim.keymap.set, the principle is the same: make the editor adapt to you, not the other way around.
Key takeaways:
Always use non-recursive mapping variants (
nnoremap,inoremap, etc.) to avoid unexpected behavior from nested mappings.The leader key (commonly Space) provides a dedicated namespace for personal mappings that do not conflict with built-in commands.
Mode-specific mappings let you assign different behaviors to the same key depending on whether you are in Normal, Insert, Visual, or Command-line mode.
Use
:verbose map <key>to debug mappings and discover which file defined a particular binding.
Exercises
Create a quick-save mapping — Map
<leader>win Normal mode to save the current file using:update. Test that it only writes when the file has actually changed.Set up the leader key — Configure Space as your leader key and create three mappings:
<leader>qto quit,<leader>bnto go to the next buffer, and<leader>bpto go to the previous buffer.Visual mode indent mapping — Create mappings so that pressing
<or>in Visual mode indents the selection without losing the visual selection. Test by selecting multiple lines and indenting them repeatedly.Mode-specific escape mapping — Create a mapping that lets you press
jkquickly in Insert mode to return to Normal mode. Verify that it does not interfere with typing the charactersjandkslowly in sequence.Debug a mapping conflict — Use
:verbose nmapto list all Normal mode mappings. Find out which file defined the mapping for<C-n>(or any key of your choice). Create a new mapping that overrides it, and verify the override with:verbose map.
Last updated
Was this helpful?