% means every line, g means every match on each line.Replace every instance of “hello” with “goodbye” in one command: :%s/hello/goodbye/g
hello world say hello to substitution hello, hello, and hello again some lines have no greeting at all one final hello before you go
Canonical solution: :%s/hello/goodbye/g Enter · par: 20 keystrokes (vimgolf rules — every keypress counts).
Anatomy of the command — : enters command mode, % means “every line”, s is substitute, /old/new/ is the swap, and g means “all matches on each line”, not just the first.
| Keys | What it does |
|---|---|
| :%s/old/new/g | replace all occurrences in the file |
| :%s/old/new/gc | same, but confirm each replacement |
| :s/old/new/ | replace the first match on the current line only |
| :%s/old/new/gi | case-insensitive replace |
Reading about keystrokes doesn't build keystrokes. Try this in a real vim buffer right now — the “The great goodbye” mission takes about a minute.
Practice free — no signup →