When it comes to learning things, I’m a big fan of layered approach. By this I mean a methodology of study that emphasizes the history of evolution of whatever it is we are learning. Recently I’ve been applying it to Unix text editors. This post is a document of my research into this topic.
As you may or may not know, in Unix operating systems there is a line of text editors that lead to creation of famous vim. The full line looks as follows:
- ed — “the standard text EDitor” written by Ken Thompson for the first Unix back in 1971
- em — “the ed for Mortals” — a mod of ed featuring one-line visual editing, developed by George Coulouris in 1976
- ex — an “eXtended” version of em written by Bill Joy at Berkley University in 1977, featuring the full-screen visual editing mode
- vi — a hard link to ex 2.0 that starts the editor with the visual mode turned ON by default
- vim — a “vi iMitation” developed in 1988-91 for Amiga computers. Supports syntax highlighting, various plugins, mouse, unlimited undo, word-completion etc.
You can find more vi history in wikipedia.
What I want to look at today is the great-great-grandfather of vim: the venerable ed. Using ed is like writing with ink using a goose pen. Its Zen character hides itself under the surface of false simplicity. It requires concentration and focus. It teaches you the origins of the commands you will see in vim. Try using ed for a day to edit all your text files.
If you’re running Mac or any flavor of Unix, you already have it. If you are on Windows, you can get it from here.
Open it on command line like this: “ed 1.txt”.
At first you can be shocked: No only you don’t see scrollbars, there is no fucking text visible anywhere on screen! That’s because ed is a command-centric editor. You issue commands to move in the files and to edit text, one line at a time. Here, to make your experience less shocking I’ve organized ed commands into 8 layers, each building up on the previous. Once you have tried them all, you will not only understand vim better, but also get the taste of what computers felt like back in 1972.
Layer 1. Moving around
- h — help explain why ed said “?”
- q — quit
- + — go to next line
- - — go to prev line
- +10 — go 10 lines forward
- -10 — go 10 lines backward
- 123 — go to line 123
- $ — go to last line
- [Enter] — move through text printing one line at a time
- z — move through text printing one screen at a time
Layer 2. Editing text
- a — append text after current line
- . — exit the text mode and go back to command mode
- p — print the current line
- d — delete current line
- c — change the text of the current line
- i — insert text before current line
- Q — quit even if there are unsaved changes
- w — write the file back to disk
Layer 3. Search and replace
- ?abc — go to the previous line containing “abc”
- /abc — same as ? but searches down, and wraps over to the top of the file
- s/abc/xyz — if current line contains “abc”, substitutes it for “xyz”
- s — substitutes the next match on the same line
- s/abc/xyz/3 — substitutes third match on the current line, instead of the first one
- s/abc/xyz/g — substitutes all matches on the current line
Layer 4. Line addressing
- 123a — append text after line 123
- 123i — insert text before line 123
- 10,20d — delete lines from 10th to 20th
- 10,20c — delete lines from 10th to 20th and enter the text that will go in their place
- 10,20p — print lines from 10th to 20th
- 10,20n — print lines from 10th to 20th with their line numbers
- %p — print all lines
- 10,20s/abc/xyz/g — substitute all occurrences of “abc” with “xyz” in lines 10 through 20
- %s/abc/xyz/g — substitute all occurrences of “abc” with “xyz” in the entire file
Layer 5. Regular expressions
The substitute command “s/abc/xyz” supports regular expressions in its “abc” part:
- . means any character. To search for dot, use \.
- .* means any sequence of any characters
- ^ means beginning of line
- $ means end of line
Also, it supports special characters in the “xyz” part:
- & means the actual substring matched by the “abc”
- \[Enter] means a new line
Layer 6. Advanced editing
- j — join the next line to the end of the current line
- m123 — move current line in front of line 123
- t123 — copy current line in front of line 123
(the above commands also work with multiple lines)
- u — undo the last command. doing the “u” again redoes the undone command.
- 123kx — mark line 123 with label “x”
- ‘x= — print line number of line marked with label “x”
- ‘x — go to line marked with label “x”
Layer 7. Interaction with external world
- !dir — execute shell command “dir” without exiting to shell
- r !dir — execute shell command “dir” and append its output to the end of the current text
- 10,20w 2.txt — write lines 10 through 20 to file 2.txt
- 10,20w !cmd — send lines 10 through 20 to standard input of shell command cmd
- f 2.txt — changes current file name to 2.txt
Layer 8. Interactive search
- G/abc — for each line matching “abc”, makes it current and allows user to enter a command
