Find & Replace in Vim

From the Vim cheat sheet · Search & Replace · verified Jul 2026

Find & Replace

Substitution and replacement commands

text
:s/old/new/ - Replace first on current line
:s/old/new/g - Replace all on current line
:s/old/new/gc - Replace all on line with confirmations
:%s/old/new/g - Replace all in file
:%s/old/new/gc - Replace all in file with confirmations
:5,10s/old/new/g - Replace all between lines 5-10
:'<,'>s/old/new/g - Replace all in visual selection

# Flags:
g - Global (all occurrences)
c - Confirm each replacement
i - Case insensitive
I - Case sensitive

# Special patterns:
:%s/^/text/ - Add text to beginning of all lines
:%s/$/text/ - Add text to end of all lines
:%s/\s\+$// - Remove trailing whitespace
:%s/\<word\>/new/g - Replace whole word only
💡 Use %s for whole file, s for current line
⚡ Add c flag for interactive replacement
📌 Visual select then :s for selection only
🟢 Remember :%s/old/new/g for global replace
searchreplacesubstitution

More Vim tasks

Back to the full Vim cheat sheet