Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am able to pattern match a regex in vi, but when I add the replacement string, vi thinks that is part of the regex match. This occurs despite use of various different delimiters. Is there some way to avoid this? As an example, the following command matches and deletes the desired string (\a{b}[c]):

s:\\a{b}[c\]

However, If I add a replace string,

s:\\a{b}[c\]:abc

I receive the error

E486: Pattern not found: \\a{b}[c\]:abc

vi is therefore not recognizing the delimiter that separates the query regex from the replacement string. This happens with a variety of different separators/delimiters. How can I ensure that vi correctly distinguishes between regex pattern and replacement string?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

The left bracket ([) starts a collection, or a set of characters that will match any one of its members. A collection is terminated by a right bracket (]), but you have backslash-escaped the right bracket in your pattern so vi sees everything to the right of the left bracket as being in the collection.

If you want to match a literal left bracket, you'll have to escape it with a backslash. If instead you meant to create a collection containing a c and a \, you'll need to backslash-escape the \. In other words, your substitute commands should probably look either like this

s:\\a{b}\[c\]:abc

or like this:

s:\\a{b}[c\\]:abc
share|improve this answer
    
Thanks, I didn't realize that by escaping the ] that I was effectively creating an unterminated character class. I only escaped that character in the first place because I couldn't achieve a match otherwise. Since I was able to match without escaping the left bracket, I did not consider escaping it as well. –  user001 Feb 22 '12 at 0:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.