7

Why do you have to escape some metacharacters in their regex engine, but not others? For example:

/foo[1-9]*  

works as expected, but the regular expression

foo[1-9]+  

must be expressed as

/foo[1-9]\+  

in vim. Anybody know?

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
Yuzu
  • 73
  • 6
  • 2
    I can't tell you why, but [here is a list](http://vimregex.com/#metacharacters) of which ones to escape if you need it. I would imagine though that it's some kind of backward compatibility thing. That in the beginning only a few meta-characters were supported and everything else was literals. And then they added the other meta-characters, but still wanted old patterns (which might include `+` as a literal) to work. So all new ones have to be escaped. But that's just a wild guess. – Martin Ender Dec 22 '12 at 00:41
  • Backwards comparability with new regex metacharacters would make sense. I'm curious as to why they didn't just force users to comply after regex got fairly standardized if that is the case. Thanks for the input though! – Yuzu Dec 22 '12 at 00:47

1 Answers1

26

This is because vim (actually vi) created their own regex flavor long before perl did. Even POSIX BRE and ERE came after vimwikipedia. They are still maintaining their own flavor so it's quite different.

To make the answer more resourceful here is a quote from ed's wiki.

The editor was originally written in PDP-11/20 assembler in 1971 by Ken Thompson. Many features of ed came from the qed from his alma mater University of California at Berkeley3 Thompson was very familiar with qed, and had reimplemented it on the CTSS and Multics systems. His versions of qed were the first to implement regular expressions. Although regular expressions are part of ed, their implementation is considerably less general than that in qed.

Aspects of ed went on to influence ex, which in turn spawned vi. The non-interactive Unix command grep was inspired by a common special uses of qed and later ed, where the command g/re/p means globally search for the regular expression re and print the lines containing it. The Unix stream editor, sed implemented many of the scripting features of qed that were not supported by ed on Unix. In turn sed influenced the design of the programming language AWK - which inspired aspects of Perl.

These two paragraphs have a lot of information! I wish I could bold it all. Some highlights,

  • Ken Thompson wrote ed in 1971. ed was actually a reimplentation of qed.
  • Ken added Regular Expression in his version of qed which is actually ed.
  • Inspired by ed, in 1976 William Joy (known as Bill Joy) wrote exwikipedia
  • Bill Joy in 1976 wrote vi as the visual mode for a line editor called ex wikipedia
  • grep was inspired by special uses of qed and later ed.
  • sed was implemented as many of the scripting features of qed that were not supported by ed on Unix
  • sed influenced the design of awk.

So vi Regular Expression were in ed which was written in 1971. It's long before any other regular expression flavor.

Community
  • 1
  • 1
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • Ah, I see. Huh. I always thought POSIX ERE was the hard and fast standard for regex and never knew that it came in different flavors until today. It seems like it, with pages like [this](http://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm) which should more appropriately be prefaced with a big "THIS GUIDE APPLIES TO POSIX ERE REGULAR EXPRESSIONS ONLY." Thanks for the answer man! – Yuzu Dec 22 '12 at 01:14
  • Update my answer a little bit. See the timeline you'll get a lot of idea. – Shiplu Mokaddim Dec 22 '12 at 01:36