LockerGnomeTech news and nostalgia for today's technology professionals
Subscribe

Search

Retrogreptech-historyunixken-thompsonbell-labs

Grep Is Named After the Ed Editor Command g/re/p, and Ken Thompson Built It in About an Hour

Ken Thompson turned a private search tool into grep overnight for a Bell Labs colleague, and a 1974 Unix tape recovered in 2025 still opens its source with the line "grep -- g/re/p".

Lee McMahon had a megabyte of text and an editor that couldn’t hold it. He was a Bell Labs researcher trying to work out who wrote which of the Federalist Papers, and the Unix editor of the day, ed, loaded whole files into a PDP-11’s tiny memory.

Ken Thompson fixed that in one evening. The tool he handed over is the one you still type every day.

In a watercolor scene on fan-fold printer paper, Chris pulls a long strip of printout toward him and holds a magnifying glass over one highlighted line.
Illustration: LockerGnome

McIlroy, who ran the department, wrote the short version in 1987. “Grep, generally cited as the prototypical software tool, was born when Ken Thompson was asked how to search for patterns in a file that was too big for the editor. Thompson promptly liberated his regular expression recognizer and christened it after the ed command g/re/p. It was an instant hit and soon entered our language as a verb.”

In ed, g/re/p means run through the whole file (g), find lines matching a regular expression (re), and print them (p). Thompson cut that one command out of the editor and made it a program that read files as a stream, so size stopped mattering.

Brian Kernighan tells the longer version in a Computerphile interview. “One of my colleagues at the time, Lee McMahon, was very interested in doing text analysis,” he said. The Federalist Papers were “published anonymously under the name Publius,” and all “85 of these documents was in total just over a megabyte.” McMahon “couldn’t edit them all in ‘ed’.”

McMahon “said this to Ken Thompson and then went home for dinner or something like that. And he came back the next day and Ken had written the program,” Kernighan said. “The original ‘grep’ was written in PDP 11 assembly language.”

Thompson remembers it differently. In a 2019 conversation with Kernighan at the Vintage Computer Festival East, he says he already had a private search program sitting in his own directory, named s, and used it constantly. McIlroy, his department head, came in wishing out loud for a way to look for things in files. Thompson said he’d think about it overnight, spent about an hour cleaning up s, and gave McIlroy grep the next morning.

Play the video on YouTube

That leaves two requesters, McMahon in Kernighan’s telling and McIlroy in Thompson’s, and an “overnight” job that was mostly a rename of a tool that already worked.

The 1974 source survives. The Unix Heritage Society describes the Utah tape as “a snapshot copy of Fourth Edition Unix sent to the University of Utah on tape in June 1974. The tape was found in 2025 and sent to the Computer History Museum to recover the contents.” The raw images are on the Internet Archive.

On that tape, usr/source/s1/grep.s is 506 lines of PDP-11 assembly. Its first line reads / grep -- g/re/p. The next one is the usage line: / grep [ -v ] [ -l ] [ -n ] re [ input ] ... Invert the match, list file names, number the lines. All three flags still work the same way.

Kernighan used the story as a homework assignment. In spring 1993 he had Princeton students turn ed’s “1800 lines of C” into grep in a week. “None of them were Ken Thompson,” he said.

The tools built this way keep getting rebuilt rather than replaced. Newer searchers such as ripgrep still answer to -v, -l and -n and print one line per match, and the next generation of code-search tools for AI agents will most likely do the same, because the scripts and the habits came first.

Who walked into Thompson’s office first is still open. Thompson says McIlroy, Kernighan says McMahon, and McIlroy’s own written account puts it in the passive voice: Thompson “was asked.”

More Retro

Every Retro story →