Say I have a text containing the words red
and blue
.
How do I replace occurences of the word blue
with the word green
only in all lines containing the word red
?
Likewise how can I replace blue
with green
in all lines NOT containing the word red
?
To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring. The string 3foobar4 matches the regex /\d. *\d/ , so it is replaced.
To replace "blue" with "green" in lines that contain "red":
:g/red/s/blue/green
To do the replacement in lines that do not contain "red":
:g!/red/s/blue/green
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