0

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?

1 Answer 1

4

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
1
  • 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, 2012 at 0:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .