Xiah
Xiah

Reputation: 31

Search/replacing multiple lines using VI

I'm trying to edit a file on Ubuntu using VI so I can search/replace using two lines. The lines are often on different lines for each file (So i can't use the search by line) and the text i'm looking for is

[wan2]
#disable = yes

They are always one after each other and i'm looking to change it from the above to the below

    [wan2]
disable = yes

I've tried a few different ways around this but I keep getting the below:

E486: Pattern not found: [wan2]\n#disable = yes 

Below and a few slight alterations is about as close as I came, I hope this is something stupid I can fix but i'm slightly stumped.

:s/[wan2]\n#disable = yes /[wan2] \ndisable = yes

Upvotes: 1

Views: 41

Answers (1)

Anthony Geoghegan
Anthony Geoghegan

Reputation: 12003

Ex command

The most reliable way is to use the Ex command:

:/wan2/+1s/^#//

This runs s/^#// on the line following the occurrence of the string wan2 indicated by /wan2/+1. As an Ex command this has the advantage of working in all Vi implementations (not just Vim).


Vim substitution

Your Vim substitution command didn’t have the square brackets escaped so the [wan2] part regex would be interpreted as a character collections for matching. Also, the trailing space after “yes” may have prevented a match. The following should also work for you in Vim:

:s/\[wan2\]\n#disable = yes/[wan2]\rdisable = yes/

Upvotes: 1

Related Questions