VIM QUICK REFERENCE
Modes, navigation, editing, search, macros, buffers
Modes
Mode Switching
| i | Insert mode (before cursor) |
| a | Insert mode (after cursor) |
| o | Insert line below and enter insert mode |
| O | Insert line above and enter insert mode |
| v | Visual mode (character) |
| V | Visual mode (line) |
| Ctrl+v | Visual block mode |
| : | Command-line mode |
| R | Replace mode |
| Esc | Return to normal mode |
Navigation
Character & Line Motion
| h j k l | Left, down, up, right |
| 0 / ^ | Start of line / first non-blank |
| $ | End of line |
| w / W | Next word / WORD |
| b / B | Previous word / WORD |
| e / E | End of word / WORD |
| f{c} / F{c} | Find char forward / backward |
| t{c} / T{c} | Till char forward / backward |
Screen & File Motion
| gg / G | Go to first / last line |
| :{n} | Go to line n |
| Ctrl+d / Ctrl+u | Half-page down / up |
| Ctrl+f / Ctrl+b | Full page down / up |
| H / M / L | Top / middle / bottom of screen |
| % | Jump to matching bracket |
Editing
Insert & Append
| I | Insert at beginning of line |
| A | Append at end of line |
| ea | Append at end of word |
| gi | Insert at last insert position |
Delete & Change
| x / X | Delete char under / before cursor |
| dd | Delete line |
| dw | Delete word |
| d$ / D | Delete to end of line |
| cc / S | Change entire line |
| cw | Change word |
| c$ / C | Change to end of line |
| ci" / ci( | Change inside quotes / parens |
Copy & Paste
| yy | Yank (copy) line |
| yw | Yank word |
| y$ | Yank to end of line |
| p / P | Paste after / before cursor |
| J | Join line below to current line |
Undo & Repeat
| u | Undo |
| Ctrl+r | Redo |
| . | Repeat last command |
Search & Replace
Search
| /pattern | Search forward |
| ?pattern | Search backward |
| n / N | Next / previous match |
| * | Search word under cursor (forward) |
| # | Search word under cursor (backward) |
Substitute
:s/old/new/ " replace first on line
:s/old/new/g " replace all on line
:%s/old/new/g " replace all in file
:%s/old/new/gc " replace all, confirm each
Visual Mode
Selection
| v | Start character selection |
| V | Start line selection |
| Ctrl+v | Start block selection |
| o | Move to other end of selection |
| gv | Reselect last visual selection |
Actions on Selection
| d | Delete selection |
| y | Yank selection |
| c | Change selection |
| > | Indent selection |
| < | Unindent selection |
| ~ | Toggle case |
| U / u | Uppercase / lowercase selection |
Buffers & Windows
Buffers
| :ls | List open buffers |
| :bn / :bp | Next / previous buffer |
| :b{n} | Go to buffer n |
| :bd | Close current buffer |
| :e file | Open file in new buffer |
Windows
| :sp file | Horizontal split |
| :vsp file | Vertical split |
| Ctrl+w h/j/k/l | Navigate to window |
| Ctrl+w = | Equalize window sizes |
| Ctrl+w q | Close window |
Tabs
| :tabnew file | Open file in new tab |
| gt / gT | Next / previous tab |
| :tabclose | Close current tab |
Registers
Using Registers
| "ay | Yank into register a |
| "ap | Paste from register a |
| "Ay | Append yank to register a |
| :reg | Show all registers |
Special Registers
| "" | Unnamed (last delete/yank) |
| "0 | Last yank |
| "+ | System clipboard |
| "/" | Last search pattern |
| ". | Last inserted text |
| "_ | Black hole (discard) |
Macros
Recording & Playing
| q{a} | Start recording macro into register a |
| q | Stop recording |
| @{a} | Play macro from register a |
| @@ | Replay last macro |
| 5@a | Play macro a 5 times |
Macro Example
qa " start recording into a
I// " insert // at line start
Esc j " return to normal, move down
q " stop recording
10@a " comment next 10 lines
Settings
Common Options
:set number " show line numbers
:set relativenumber " relative line numbers
:set tabstop=4 " tab width
:set expandtab " spaces instead of tabs
:set ignorecase " case-insensitive search
Display & Behavior
:set wrap / nowrap " toggle line wrapping
:set hlsearch " highlight search matches
:set incsearch " incremental search
:set cursorline " highlight current line
:syntax on " enable syntax highlighting
File Operations
Save & Quit
| :w | Save |
| :w file | Save as file |
| :q | Quit (fails if unsaved) |
| :q! | Quit without saving |
| :wq / :x / ZZ | Save and quit |
| :wa | Save all buffers |
| :qa | Quit all buffers |
File Info & External
| Ctrl+g | Show file info |
| :!cmd | Run shell command |
| :r !cmd | Insert command output |
| :r file | Insert file contents |
Text Objects
Inner & Around Objects
| iw / aw | Inner / around word |
| is / as | Inner / around sentence |
| ip / ap | Inner / around paragraph |
| i" / a" | Inner / around double quotes |
| i' / a' | Inner / around single quotes |
| i( / a( | Inner / around parentheses |
| i{ / a{ | Inner / around braces |
| it / at | Inner / around HTML tag |
Common Combos
ciw " change inner word
di" " delete inside quotes
ya{ " yank around braces
vip " select inner paragraph
Marks & Jumps
Marks
| m{a} | Set mark a at cursor position |
| 'a | Jump to line of mark a |
| `a | Jump to exact position of mark a |
| :marks | List all marks |
Jump List
| Ctrl+o | Jump to previous position |
| Ctrl+i | Jump to next position |
| :jumps | Show jump list |
| '' | Jump to last jump position |