search

:s and :%s

Substitute (find-and-replace) within a range of lines. Without a range :s works on the current line only; :%s uses % as shorthand for the whole file (equivalent to :1,$s). Add flags like g (all matches per line) and c (confirm).

:[range]s/{pattern}/{replacement}/[flags]     :%s/…/g  (command mode)

Variations

Keystroke Effect
g All occurrences on each line (not just the first)
c Confirm each replacement (y/n/a/q/l)
i Case-insensitive for this substitution
% Range shorthand for the whole file (:%s = :1,$s)
'<,'> Range that means 'the last visual selection'

Examples

:s/foo/bar/

Replace first foo with bar on current line

:%s/foo/bar/g

Replace every foo with bar in the whole file

:%s/\s\+$//

Strip trailing whitespace from every line

:%s/foo/bar/gc

Interactive replace across file, confirm each match

Gotcha

Without the g flag only the FIRST match per line is replaced — a classic 'why didn't it work' moment. Note: % here means 'whole file', but the SAME % key in normal mode jumps to a matching bracket — mode context matters.

Related

← All Vim commands · Vim cheat sheet