Vi Command Patterns
Vi has a logical grammar once you see it. Let me break down the patterns:
The Core Grammar: Operator + Motion
Most vi commands follow this structure:
[count] + operator + motion
Operators (what to do):
d= deletec= change (delete + enter insert mode)y= yank (copy)
Motions (where/how far):
w= word forwardb= word backward$= end of line0= beginning of lineG= end of filegg= beginning of file
Combine them:
| Command | Meaning |
|---|---|
dw | delete word |
d$ | delete to end of line |
dG | delete to end of file |
cw | change word |
c$ | change to end of line |
y$ | yank to end of line |
Double Letter = Whole Line
When you double an operator, it acts on the entire current line:
dd= delete lineyy= yank linecc= change line
Uppercase = Stronger/Bigger Version
Uppercase letters are often shortcuts for “to end of line” or a more powerful variant:
| Lowercase | Uppercase | Pattern |
|---|---|---|
d (needs motion) | D = d$ | delete to end of line |
c (needs motion) | C = c$ | change to end of line |
o (open line below) | O (open line above) | opposite direction |
p (paste after) | P (paste before) | opposite position |
a (append after cursor) | A (append at end of line) | bigger scope |
i (insert at cursor) | I (insert at line beginning) | bigger scope |
n (next search forward) | N (next search backward) | opposite direction |
g (prefix key) | G (go to last line) | end of file |
Counts Multiply Everything
Put a number before any command to repeat it:
5j= move down 5 lines3dd= delete 3 lines4yy= yank 4 lines2dw= delete 2 words
Mnemonics — The Letters Mean Something
d= deletec= changey= yank (copy)p= put (paste)w= wordb= backr= replaceu= undoo= open linea= appendi= insertx= think of it as crossing out a characterJ= Join linesG= Go to line
Search Pattern
/pattern= search forward (slash points right/down)?pattern= search backward (question = going back to ask again)n= next match (same direction)N= next match (opposite direction)
Scrolling — Directional Letters
Ctrl+d= down half pageCtrl+u= up half pageCtrl+f= forward full pageCtrl+b= back full page