Why []([a].*) matches [](b)? I would expect it to require a to be the first character in ().

1 Answer 1


This is happening because Vim is actually seeing all the characters in between the very first [ and the last ] as a single character class that will match any one of ], (, [, or a.

In other words, what Vim is seeing here is []([a].*), with the bold part being the character class. So it's matching any of these four characters, followed by everything until a ).

This happens because, in order to match a literal ] in a character class, it needs to be the first character listed in the character class. So if you only have a [] an no other character class in the regexp (for example [](.*)), Vim will find that to be an invalid character class and match those characters literally. But as soon as you add another ] somewhere later in that regexp, you're now making it valid and making it match any character in between.

You can easily fix it by adding a backslash before the first [, to force it to match a literal bracket: \[]([a].*).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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