The g option (e.g. s/pattern/replacement/g) for many tools that use regex-style pattern matching and the :g command in ed, ex, vi, and 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 pattern-matching flag, and in which tool? It looks like most tools (such as sed) that use the g flag 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 flag 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 flag 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.

migrated from cs.stackexchange.com Feb 18 '14 at 19:58

This question came from our site for students, researchers and practitioners of computer science.

  • 2
    qed had a G command and its S command didn't have a g flag. It looks like the behavior of the S command was the same as what we've come to know as ed's s command with the g flag. cm.bell-labs.com/who/dmr/qedman.html says, for the S command, "Occurrences of <regexp> in the addressed lines are replaced by <string>" – Mark Plotnick Feb 19 '14 at 19:46
  • @MarkPlotnick Well, that answers part one! Nicely done. – Kyle Strand Feb 19 '14 at 20:39
  • 2
    New URL: bell-labs.com/usr/dmr/www/qedman.html – Scott Apr 4 at 20:57

vi is inspired by ex, ex is inspired by ed, ed is inspired by qed

QED was hacked together by Ken Thompson way back in the late 1960's for MIT's "Compatible Time-Sharing System" (a previous version for the Berkeley Timesharing System was created by Butler Lampson, L. Peter Deutsch, and Dana Angluin) — in short Thompson added regex in qed (he did a lot more than that, but it's outside the scope of this answer. -- Bell Labs has more on the history of QED)

One command in qed was the "G" or "Global" command. It allowed you to operate on all lines in the file at once (the previous version of qed were character-oriented instead of line oriented.)

Grep is actually named for one of the uses of this command G/re/P (G global, re regular expression, P print) in qed this command was used like G/bash/P to print out all lines containing the word bash — this was later included in ed, then taken out of ed and made into a standalone function (according to Doug McIlroy, he asked Ken to do it for him & Ken left it on his desk the next morning)

  • As pointed out by Mark Plotnick in his comment on the question, qed actually had the g command, too. – Kyle Strand Feb 19 '14 at 22:36

I believe they both came at the same time as part of ed, or possibly QED. They're pretty fundamental to the usage of ed. If there were ever a version that didn't have both, I can't imagine how it would function.

g// and //g are two completely different things. And there are actually several things going on here.

  • // is for searching for the next line in the file that contains the pattern, it can only return a single line (note that 1,$/./ is invalid).
  • g// is for searching for a pattern on multiple lines, by default all, or a specified range. It can return multiple lines.
  • //g alone is meaningless and not a valid command in ed (although some other engines accept it, it's functionally the same as // anyway).
  • s// is for replacing the first instance of a pattern on the current (or specified) line(s).
  • s//g is for replacing the all instances of a pattern on the current (or specified) line(s).

I hope that this at least somewhat clears up the confusion. But I think the main point of the answer to your question is that those are two different things. Each is used for a different reason and mean different things.

  • First, qed didn't have any regex support; that was (according to a couple different versions of the story) the primary leap forward with ed. So I'm not sure what you mean about not being able to imagine how such an editor "would function," since there are plenty of other editing commands. – Kyle Strand Feb 18 '14 at 23:24
  • Second, I am aware of the differences, and I think my question indicates that awareness; in particular, I said that the two things "have pretty similar usage and meaning." This is, presumably, why they share the same letter even though in one case g is the command name (and is explicitly abbreviated from "global" in the help documentation) and in the other it's a flag (and, as far as I know, is not explicitly said to be an abbreviation for "global" in any editor documentation--though Programming Perl, for instance, does make this association). – Kyle Strand Feb 18 '14 at 23:25
  • Anyway, thanks for your explanation of some of the different ways regex can be used in editor commands. I think that by being somewhat vague in my terminology I gave the impression of being more confused by usage than I actually am, since I actually feel like I have a fairly solid understanding of how to use regex in Vim, sed, Perl, etc; I just want to understand the history a little better. I've made a minor edit to the original question to try to clarify this. – Kyle Strand Feb 18 '14 at 23:35

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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