AskDB
·6 min read

Vim Commands Cheat Sheet

Vim is a powerful modal text editor. Learning its keybindings dramatically speeds up text editing. This cheat sheet covers the essentials.

Modes

Esc         # Normal mode (from any mode)
i           # Insert mode (before cursor)
a           # Insert mode (after cursor)
o           # Insert mode (new line below)
O           # Insert mode (new line above)
v           # Visual mode (character)
V           # Visual mode (line)
Ctrl+v      # Visual block mode
:           # Command mode

Navigation

h j k l     # Left, Down, Up, Right
w           # Next word
b           # Previous word
e           # End of word
0           # Start of line
$           # End of line
^           # First non-blank character
gg          # First line
G           # Last line
5G          # Go to line 5
%           # Matching bracket
Ctrl+d      # Half page down
Ctrl+u      # Half page up

Editing

x           # Delete character
dd          # Delete line
D           # Delete to end of line
dw          # Delete word
yy          # Copy line
yw          # Copy word
p           # Paste after
P           # Paste before
u           # Undo
Ctrl+r      # Redo
.           # Repeat last command

Searching

/pattern    # Search forward
?pattern    # Search backward
n           # Next match
N           # Previous match
*           # Search word under cursor
:%s/old/new/g  # Replace all in file
:s/old/new/g   # Replace all in selection

Visual Mode

v           # Character select
V           # Line select
Ctrl+v      # Block select

# Then:
d           # Delete selection
y           # Copy selection
>           # Indent
<           # Unindent
=           # Auto-indent

Splits and Tabs

:sp file    # Horizontal split
:vsp file   # Vertical split
Ctrl+w h/j/k/l  # Navigate splits
Ctrl+w =    # Equalize splits
:tabnew     # New tab
gt / gT     # Next/prev tab

Useful Commands

:w          # Save
:q          # Quit
:wq         # Save and quit
:q!         # Quit without saving
:e file     # Open file
:set number # Show line numbers
:set relativenumber  # Relative numbers

Quick Reference

  • i before cursor, a after cursor
  • dd delete line, yy copy line
  • u undo, Ctrl+r redo
  • / search, n next match
  • :wq save and quit