Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
    Post Deleted by Community
    Post Locked by Community
    Post Migrated Away to by Yuval Filmus, Gilles
    Post Closed as "off-topic" by Yuval Filmus, Gilles
source | link

Regex `/pattern/g` and ed `:g/pattern/`: which came first, and why `g`?

The g option (/pattern/g) for many tools that use regex-style pattern matching and the :g command in ed/ex/vi/vim have pretty similar usage and meaning: match the given regex "globally", i.e., don't stop after the first match.

I have a two-fold question about this:

  • Which came first, the g: command or the //g option, and in which tool? It looks like most tools (such as sed) that use the g option in their pattern-matching are really just directly or indirectly emulating ed. For instance, in the post-Perl age, most tools that use regex allow the g option because Perl does, and Perl, it would appear, does it because ed -> sed -> Perl. So I wouldn't be surprised if the answer is "they were introduced simultaneously in the original ed tool, and have no historical precedent beyond that."
  • Why is this called the global option (or command)? There's really nothing "global" about it; the :g command takes a range of lines just like any other ed command, and the /g option doesn't extend the range of the search in any way (it just allows multiple hits). I suppose I can't think of a better name, but the chosen one just seems odd to me, so I'm wondering if there's some reason for it I'm not seeing.

P.S.: I've tagged this question with "regular-expressions" even though it's not exactly about regular expressions, since it does have a little bit to do with the historical basis for a particular usage of regex in various tools.