Replacing text¶
to replace text you use :s/ in command mode followed by {pattern}/ to search for the pattern, followed by {replacement}/ to replace all found instances
By prepending s with % you can replace occurrence in entire document,
likewise by appending g at end of command you can replace multiple occurrences in same line (g for global)
by appending c you can confirm each replacement before the replacement occurs
- :s/{pattern}/{replacement}/
- replace first occurrence of pattern with replacement in current line
replace multiple occurrences¶
- :s/{pattern}/{replacement}/g
- replace all occurrences of pattern in current line (prepend s with % for all lines)
confirm replacements¶
- :s/{pattern}/{replacement}/c
- confirm replacement before replacing (likewise can be combined with % or g)
replace pattern in multiple lines:¶
- :a,b + s/{pattern}/{replacement}/
- replace first occurrence of pattern with replacement in range from line a to line b
- :%s/{pattern}/{replacement}/
- replace first occurrence of pattern with replacement in all lines
- likewise if you are in visual mode then after highlighting desired range you can type : followed by s/{pattern}/{replacement} and pattern will get replaced with replacement in specified range
combine specifiers¶
- :%s/{pattern}/{replacement}/gc
- g
- allow for multiple replacements in same line
- %
- c