Vim

Essential vim keyboard shortcuts and commands.

shortcuts
vimeditorterminalshortcuts

Modes

i           # Enter insert mode at cursor
I           # Enter insert mode at beginning of line
a           # Enter insert mode after cursor
A           # Enter insert mode at end of line
o           # Open new line below and enter insert mode
O           # Open new line above and enter insert mode
v           # Enter visual mode (character selection)
V           # Enter visual line mode
Ctrl+v      # Enter visual block mode
Esc         # Return to normal mode
:           # Enter command mode
R           # Enter replace mode
h           # Move left
j           # Move down
k           # Move up
l           # Move right
w           # Move forward to start of next word
b           # Move backward to start of previous word
e           # Move to end of word
0           # Move to beginning of line
^           # Move to first non-whitespace character
$           # Move to end of line
gg          # Move to first line of file
G           # Move to last line of file
:n          # Move to line n (e.g., :42)
nG          # Move to line n (e.g., 42G)
%           # Jump to matching bracket/parenthesis
Ctrl+f      # Page down
Ctrl+b      # Page up
Ctrl+d      # Half page down
Ctrl+u      # Half page up
H           # Move to top of screen
M           # Move to middle of screen
L           # Move to bottom of screen

Editing

x           # Delete character under cursor
X           # Delete character before cursor
dd          # Delete (cut) entire line
dw          # Delete (cut) word
d$          # Delete (cut) to end of line
d0          # Delete (cut) to beginning of line
yy          # Yank (copy) entire line
yw          # Yank (copy) word
y$          # Yank (copy) to end of line
p           # Paste after cursor
P           # Paste before cursor
u           # Undo
Ctrl+r      # Redo
.           # Repeat last command
r           # Replace single character
cc          # Change (replace) entire line
cw          # Change (replace) word
c$          # Change (replace) to end of line
J           # Join line below to current line
>>          # Indent line right
<<          # Indent line left
==          # Auto-indent current line

Search and Replace

/pattern    # Search forward for pattern
?pattern    # Search backward for pattern
n           # Repeat search in same direction
N           # Repeat search in opposite direction
*           # Search forward for word under cursor
#           # Search backward for word under cursor
:%s/old/new/g           # Replace all occurrences in file
:%s/old/new/gc          # Replace all with confirmation
:s/old/new/g            # Replace all in current line
:5,12s/old/new/g        # Replace all in lines 5-12
/\cpattern  # Case-insensitive search (or use :set ic)

File Operations

:w          # Write (save) file
:w filename # Save as filename
:q          # Quit
:q!         # Quit without saving
:wq         # Write and quit
:x          # Write and quit (only if changes made)
ZZ          # Write and quit (shortcut)
ZQ          # Quit without saving (shortcut)
:e filename # Open file for editing
:r filename # Read file and insert after cursor
:bn         # Go to next buffer
:bp         # Go to previous buffer
:bd         # Delete (close) buffer

Visual Mode

v           # Start character-wise visual mode
V           # Start line-wise visual mode
Ctrl+v      # Start block-wise visual mode
o           # Move to other end of selection
d           # Delete selection
y           # Yank (copy) selection
c           # Change (replace) selection
>           # Indent selection right
<           # Indent selection left
~           # Toggle case of selection

Multiple Files and Windows

:sp filename    # Split window horizontally and open file
:vsp filename   # Split window vertically and open file
Ctrl+w s        # Split window horizontally
Ctrl+w v        # Split window vertically
Ctrl+w w        # Switch between windows
Ctrl+w h        # Move to left window
Ctrl+w j        # Move to window below
Ctrl+w k        # Move to window above
Ctrl+w l        # Move to right window
Ctrl+w q        # Close current window
Ctrl+w o        # Close all windows except current
Ctrl+w =        # Make all windows equal size
:tabnew         # Open new tab
gt              # Go to next tab
gT              # Go to previous tab
:tabc           # Close current tab

Marks and Jumps

m{a-z}      # Set mark at cursor (local to file)
m{A-Z}      # Set mark at cursor (global across files)
'{a-z}      # Jump to mark (beginning of line)
`{a-z}      # Jump to mark (exact position)
''          # Jump back to previous position
``          # Jump to exact previous position
Ctrl+o      # Jump to older position in jump list
Ctrl+i      # Jump to newer position in jump list

Text Objects

diw         # Delete inner word
daw         # Delete a word (including surrounding space)
di"         # Delete inside quotes
da"         # Delete around quotes (including quotes)
di(         # Delete inside parentheses
da(         # Delete around parentheses
dit         # Delete inside HTML/XML tag
dat         # Delete around HTML/XML tag
dip         # Delete inner paragraph
dap         # Delete a paragraph
ciw         # Change inner word
yiw         # Yank inner word

Macros

q{a-z}      # Start recording macro into register {a-z}
q           # Stop recording macro
@{a-z}      # Execute macro from register {a-z}
@@          # Repeat last executed macro

Configuration

:set number         # Show line numbers
:set nonumber       # Hide line numbers
:set relativenumber # Show relative line numbers
:set hlsearch       # Highlight search results
:set nohlsearch     # Disable search highlighting
:set ignorecase     # Case-insensitive search
:set smartcase      # Case-sensitive if uppercase present
:set autoindent     # Auto-indent new lines
:set tabstop=4      # Set tab width to 4 spaces
:set shiftwidth=4   # Set indent width to 4 spaces
:set expandtab      # Use spaces instead of tabs
:syntax on          # Enable syntax highlighting
:noh                # Clear search highlighting

Miscellaneous

:help command   # Get help for command
:help           # Open help
K               # Look up word under cursor in manual
:!command       # Execute shell command
:r !command     # Insert output of shell command
:%!command      # Filter entire file through shell command
:set paste      # Enable paste mode (disable auto-indent)
:set nopaste    # Disable paste mode
gf              # Go to file under cursor
Ctrl+g          # Show file info (path, lines, position)
ga              # Show ASCII value of character under cursor
g~iw            # Toggle case of word
guiw            # Make word lowercase
gUiw            # Make word uppercase