DEV Community

loading...
Cover image for Back to 1969 or Introduction to Ed - old-school text editor

Back to 1969 or Introduction to Ed - old-school text editor

Andrei Fedotov
I write here some notes
・6 min read

1969
What happened in 1969 Major News Stories include The Beatles' last public performance, on the roof of Apple Records, First Concorde test flight is conducted In France, Boeing 747 jumbo jet makes its debut, Pontiac Firebird Trans Am the epitome of the American muscle car is introduced, Woodstock attracts more than 350,000 rock-n-roll fans, Members of a cult led by Charles Manson murder five people, Chappaquiddick Affair Senator Edward Kennedy, PBS Established, The first man is landed on the moon on the Apollo 11 mission by the United States and Neil Armstrong and Edwin 'Buzz' Aldrin became the first humans to set foot on the Moon.
But there is one more thing ... Awesome string-line text editor: ed. Let's immerse in the atmosphere of the late 1960s and touch the history.

History
As early as 1969, the first assembly-language version of ed was in place. Although later rewritten in C, the editor is fundamentally the same program as used then. As Kernighan and Plauger wrote in 1976, The earliest traceable version of the editor presented here is TECO, written for the first PDP-1 timesharing system at MIT.

PDP-1:
PDP-1

It was subsequently implemented on the SDS-940 as the "quick editor'" QED by L. P. Deutsch and B. W. Lampson.

SDS-940:
Alt Text

K. L. Thompson adapted QED for CTSS on the IBM 7090 at MIT.

IBM 7090:
Alt Text

Later D. M. Ritchie wrote a version for the GE-635 at Bell Labs.
The latest version is ed, a simplified form of QED for the PDP-11, written by Ritchie and Thompson.

PDP-11:
Alt Text

This is not to say that ed is the same as the TECO found on some DEC computers. For one thing, TECO is character oriented while ed is line oriented. It seems rather a case of "common ancestry".
During the 1970's, the editor went through countless revisions. Nearly every university had its own modified versions of ed and qed; some had several modified versions. Jay Michlin of Bell Labs wrote (in IBM assembler) a QED for IBM's mainframe TSO; this was released to Universities in the mid-70's.
And, of course, ed would visit Berkeley and, while there, mutate into ex and vi.
Let's taste it.
During our today’s journey we will consider simple operations with Ed and after we will develop the script that performs the following actions: displaying a list of files that are “children” of a given directory that have a given character set in their names and destroying all other files of a given directory. In the end we will create initializing script that will say hello after login and run another script for searching files.

Simple operations of Ed
Let’s create two three-level directories for our experiments.
mkdir -p {dirA,dirB}/dir1/dir2
Now we have the following structure (can be output to console with the help of tree command):

.
├── dirA
│   └── dir1
│       └── dir2
└── dirB
    └── dir1
        └── dir2
Enter fullscreen mode Exit fullscreen mode

Create four text files in one of the directories.

cd dirA
cd dir1
cd dir2
touch file1.txt file2.txt file3.txt file4.txt

Alt Text

Adding the text to a file with ed.
Let’s edit the file with ed. Launch it with the filename as a parameter:
ed file1.txt
In case when file does not exist ed creates a new one. If it already exists, then ed tells us the length of file. In our case the length is zero. In order to add text to the file we have to switch to adding mode by “a” command.
It appends text to the buffer after the addressed line. The current address is set to last line entered.
An address represents the number of a line in the buffer. ed maintains a current address which is typically supplied to commands as the default address when none is specified. When a file is first read, the current address is set to the last line of the file. In general, the current address is set to the last line affected by a command.
So, let’s switch to adding mode:
a
And type some words:
Hello!
This is text form file1!
To exit from adding mode type point on the new line:
.
To write changes type:
w
it prints new size of file. In order to quit from ed:
q

Alt Text

Adding text to the beginning, middle and to the end of file.
Let's complicate the task a bit and add text to the beginning, middle and end of the file.
Open file again. Let’s print the addressed line by ‘p’ command. It prints us ‘This is a text from file1!’. It’s easy to add the text to the end of file right now. Just switch to adding mode by ‘a’ command and print ‘End’.
Done! Now exit from adding mode by printing ‘.’. To print the first line of document type ‘1p’. It also set the current address to the number of first line. Now we can print ‘i’ command to switch to ‘Insert mode’. This magic command inserts text before the current line and the current address is set to the last line entered. So, after switching to insert mode, let’s type ‘Start’. Then, on the new line type ‘.’ to exit from insert mode. Let’s set the current address to the line number 3. In order to do this just type ‘3p’. Now we can switch to insert mode again and add text to the middle of document.
In order to print the whole text from file type the following:
1, $p
Further save file and quit from ed. Step-by-step way is represented in the picture below.

Alt Text

Let’s copy file with the help of ‘cp’ command.

Alt Text

Super finder script
Let’s make the challenge more complicated and develop the script for searching files, which are located in all subdirectories of the current directory, which have in their name a given character set. Then display these files and destroy all other files of a given directory. The script will fart with two parameters. The first parameter is catalog name and the second is the set of symbols (mask). For testing let’s create files in the directories.

Alt Text

Run the script to execute. After check the contents of the directories.

Alt Text

Initialization script
Let's develop our initialization script, which say hello by rewriting shell invitation.
Create file “.bashsrc” and edit it in ED editor.

Alt Text

In order to check do logout and login into the system back.

Alt Text

Let's perfect our script so that it also runs the nested script created earlier and passes the directory name and part of the file name as parameters to it. And check his work.

Alt Text

So, we plunged into the atmosphere of the 1960s, I hope it was fun. That’s it. Thank you!
Cheers!

Discussion (0)