In vim, how can I replace a word only on specific lines where a string is matched? For example, I have
replace here: foo
but not here: foo
replace again here: foo
and I want to replace foo
with bar
on all lines where the string replace
is found, i.e. the output should be
replace here: bar
but not here: foo
replace again here: bar
Inspired by the sed like syntax of vim's search and replace, I'd expected
:/replace/s/foo/bar/
to work, but it does the replacement only on the first line where it matches replace
. How can I extend this to the whole document?
You can use g
command (global) for changing it in multiple lines:
:g/replace/s/foo/bar/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With