8

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.
4
  • 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>" Feb 19, 2014 at 19:46
  • @MarkPlotnick Well, that answers part one! Nicely done. Feb 19, 2014 at 20:39
  • 2
  • The ed(1) g command is global in that it applies a (normally one-line-at-a-time) command to several lines.
    – vonbrand
    Mar 1, 2020 at 2:36

2 Answers 2

8

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)

2
  • As pointed out by Mark Plotnick in his comment on the question, qed actually had the g command, too. Feb 19, 2014 at 22:36
  • Wrong. ex(1) is the (line editor) heir to ed(1), vi(1) is just the visual (full-screen) interface to ex(1). You could go to visual from ex, they were aliases (typically /usr/bin/vi and /usr/bin/ex being hard links, with the program starting in one or the other mode depending on the name with which it is called). Even today vim (VI iMproved, no relation to the original code) installed on Fedora has /usr/bin/ex as a symlink to /usr/bin/vi. The : commands to vi are ex commands.
    – vonbrand
    Mar 1, 2020 at 2:41
5

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.

4
  • 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. Feb 18, 2014 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). Feb 18, 2014 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. Feb 18, 2014 at 23:35
  • Note that you can say e.g. :20,200g/today/s/month/February/g...
    – vonbrand
    Mar 1, 2020 at 2:49

You must log in to answer this question.

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