The Ultimate vi / vim Master Guide & Reference Cheat Sheet
Whether you are SSH’ed into an emergency server recovery prompt or fine-tuning your Hugo markdown posts, vi (and its enhanced sibling vim) is an essential tool in any systems administrator’s toolkit. This guide covers everything from core movement to powerful macros and regex replacements.
Quick Reference Cheat Sheet
1. Modes & Basic Navigation
| Action |
Key Combination / Command |
| Enter Insert Mode (Type text) |
i (before cursor), a (after cursor), I (start of line), A (end of line) |
| Enter Command/Line Mode |
Esc (Press twice to be safe) |
| Move Up / Down / Left / Right |
k (up), j (down), h (left), l (right) or Arrow Keys |
| Move by Words |
w (next word start), b (previous word start), e (word end) |
| Move by Lines |
0 (start of line), $ (end of line), gg (top of file), G (bottom of file) |
| Jump to Specific Line |
:[line-number] (e.g., :42) |
2. Saving & Exiting (: commands)
| Action |
Key Combination / Command |
| Save (Write) file |
:w |
| Save and Exit |
:wq or ZZ |
| Quit without saving (Discard) |
:q! |
| Open file for editing |
vim filename |
3. Editing: Deleting, Yanking (Copying), & Pasting
| Action |
Key Combination / Command |
| Delete (Cut) current line |
dd |
| Delete N lines |
[N]dd (e.g., 5dd) |
| Delete character |
x |
| Yank (Copy) current line |
yy |
| Yank N lines |
[N]yy |
| Paste after cursor / line |
p |
| Paste before cursor / line |
P |
| Undo last action |
u |
| Redo last action |
Ctrl + r |
4. Searching & Replacing
| Action |
Key Combination / Command |
| Search forward |
/keyword (Press n for next match, N for previous) |
| Search backward |
?keyword |
| Global Search & Replace |
:%s/old/new/g |
| Search & Replace with Confirmation |
:%s/old/new/gc |
Understanding Modal Editing
The defining characteristic of vi is modal editing. Unlike modern graphical text editors where typing keys instantly inserts characters, vi separates input into distinct states:
- Normal Mode: The default state when opening a file. In this mode, every keystroke is interpreted as a command (moving the cursor, deleting lines, saving, or switching modes).
- Insert Mode: Entered by pressing
i, a, or o. In this mode, keys behave like a normal typewriter, inserting text directly into the buffer. Pressing Esc returns you safely to Normal Mode.
- Command-Line Mode (Ex Mode): Entered by typing
: while in Normal Mode. This drops your cursor to the bottom row for executing file operations, searches, and configurations.
Advanced Navigation & Text Manipulation
Text Objects and Motion Modifiers
vi commands are a language consisting of Verbs combined with Motions:
dw = Delete Word (d = delete, w = word)
caw = Change A Word (c = change/replace, aw = a word)
di" = Delete Inside Quotes (d = delete, i = inside, " = quotes)
yip = Yank Inside Paragraph (y = yank, i = inside, p = paragraph)
Visual Mode
Pressing v (character-wise), V (line-wise), or Ctrl + v (block/column-wise visual mode) allows you to highlight text using your arrow keys or navigation buttons, and then apply operations like y (yank), d (delete), or > (indent block) to the exact selection.
Power User Tips & Environment Configuration
Customizing Your .vimrc
You can persist custom preferences by creating or editing a configuration file at ~/.vimrc:
syntax on " Enable syntax highlighting
set number " Show line numbers
set tabstop=4 " Set tab width to 4 spaces
set shiftwidth=4 " Auto-indent width
set expandtab " Use spaces instead of actual tabs
set hlsearch " Highlight search results
set incsearch " Search incrementally as you type
|