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 = delete
  • c = change (delete + enter insert mode)
  • y = yank (copy)

Motions (where/how far):

  • w = word forward
  • b = word backward
  • $ = end of line
  • 0 = beginning of line
  • G = end of file
  • gg = beginning of file

Combine them:

CommandMeaning
dwdelete word
d$delete to end of line
dGdelete to end of file
cwchange 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 line
  • yy = yank line
  • cc = change line

Uppercase = Stronger/Bigger Version

Uppercase letters are often shortcuts for “to end of line” or a more powerful variant:

LowercaseUppercasePattern
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 lines
  • 3dd = delete 3 lines
  • 4yy = yank 4 lines
  • 2dw = delete 2 words

Mnemonics — The Letters Mean Something

  • d = delete
  • c = change
  • y = yank (copy)
  • p = put (paste)
  • w = word
  • b = back
  • r = replace
  • u = undo
  • o = open line
  • a = append
  • i = insert
  • x = think of it as crossing out a character
  • J = Join lines
  • G = 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 page
  • Ctrl+u = up half page
  • Ctrl+f = forward full page
  • Ctrl+b = back full page