maux
Star on GitHub Buy me a coffee

vi hands its text to other programs

Watch / vi

Filmed on the Apple IIe 80-column display. The same session on the Videx UltraTerm: same keys, same text.

The Videx UltraTerm was an 80-column video card for the Apple IIe, with its own video memory and character generator, real hardware scrolling and more screen modes than the IIe's own 80 columns. This demo runs on either display; the UltraTerm film shows the original Videx UltraTerm, emulated.

A real vi, at 80 columns, on a new file in /tmp. Its ! commands pass text to the rest of the system: :r !ls /usr/man | head -8 reads a pipeline's output into the buffer, :%!sort -r hands the whole buffer to sort and takes the answer back, :2,4!tr A-Z a-z sends only lines 2 to 4 through tr, and :%!nl numbers every line. Then the file is written, :!wc -l notes runs wc on it through a shell escape, and cat shows what is on the disk.

What it types

Everything it types, in order. A line starting with $ is a command at the shell, RET included; keys lines are single keystrokes to a program (RET, ESC and the control keys are named).

$ cd /tmp
$ vi notes
  keys: :r !ls /usr/man | head -8<RET>1Gdd/BASENAME<RET>ddGp:%!sort -r<RET>:2,4!tr A-Z a-z<RET>:%!nl<RET>:w<RET>:!wc -l notes<RET><RET>:q<RET>
$ cat notes
$ cksum notes
$ ls

Between steps it presses ^L to clear the screen.

What the captions say

  1. vi, in 80 columns. On a IIe you once asked for those with PR#3.
  2. vi on a new file in /tmp.
  3. Its ! commands hand text to other programs and take the answer back.
  4. :r !ls /usr/man | head -8 -- the output of a pipeline becomes text in the buffer.
  5. A little editing: delete the blank line, then move BASENAME to the end.
  6. :%!sort -r -- the whole buffer goes through sort, and comes back.
  7. :2,4!tr A-Z a-z -- only lines 2 to 4 go through tr.
  8. :%!nl -- line numbers, from another program.
  9. :w writes the file.
  10. :!wc -l notes -- a shell escape runs wc on it and holds the answer: 8 lines.
  11. How do you leave vi? Like this. The disk matches the screen.

The gate that proves it

This film is made by site/demos/vi.py. Run as assertions, the same file is make check-demo-vi, and it runs with the rest of the tests. After every editing step it reads the 23 rows of the editing window and requires the buffer's lines, in order, with a ~ on every row below them; the bottom status row is not read. Every cell must match exactly except one: the cell under the cursor, which the Apple paints as a different glyph for a lower-case letter, and the gate accepts only that glyph, at the cursor's position. The expected rows are computed on the host from the bytes of the image (the names in /usr/man) and what sort, tr and nl do to them; nothing about a result is typed into the demo. It requires that the held output of :!wc -l notes says 8 lines for the file just written, that cat on the file shows the numbered list in order, that cksum equals the host's checksum of the bytes the file must hold, and that no editor temporary file is left in the directory. The checks themselves were tested: with sort or tr replaced by a script that changes one letter of one line, with sort, tr, head or wc replaced by a copy of cat, and with nl, wc, ls or sh replaced by true, the demo fails at the row that uses it.

What this vi does not do yet

The filter commands are the colon forms, which vi hands to its ex engine. The classic normal-mode forms !! and !} are not implemented: ! in command mode is ignored (listed under BUGS in vi(1)), so the film uses the colon forms only. Also, :w !cmd, which pipes the buffer out to a command, does not yet hold the command's output on the screen as POSIX vi does, so the film does not show it.

Transcript

The screen at every checkpoint the test checks, as the recorder saved it:

== the buffer is a blank line and the first 8 names in /usr/man, in order (t=34.4 s)
ADMIN
AR
AS
ASA
AT
BASENAME
BATCH
BPATCH
~
~
~
~
~
~
~
~
~
~
~
~
~
~

== the blank line is gone (t=42.5 s)
ADMIN
AR
AS
ASA
AT
BASENAME
BATCH
BPATCH
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~

== the buffer ends with BASENAME (t=47.8 s)
ADMIN
AR
AS
ASA
AT
BATCH
BPATCH
BASENAME
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~

== the buffer is reverse sorted, in order (t=58.3 s)
BPATCH
BATCH
BASENAME
AT
ASA
AS
AR
ADMIN
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~

== lines 2 to 4 are lower case, the rest unchanged (t=66.4 s)
BPATCH
batch
basename
at
ASA
AS
AR
ADMIN
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~

== every line numbered, in order (t=75.8 s)
     1  BPATCH
     2  batch
     3  basename
     4  at
     5  ASA
     6  AS
     7  AR
     8  ADMIN
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~

== wc counted exactly the lines of the file: 8 (t=91.6 s)
     3  basename
     4  at
     5  ASA
     6  AS
     7  AR
     8  ADMIN
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
:!wc -l notes
8 notes
[Hit return to continue]

== back in the editor, buffer unchanged (t=94.4 s)
     1  BPATCH
     2  batch
     3  basename
     4  at
     5  ASA
     6  AS
     7  AR
     8  ADMIN
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~

== the file on disk is the numbered list (t=103.7 s)
$ cat notes
     1  BPATCH
     2  batch
     3  basename
     4  at
     5  ASA
     6  AS
     7  AR
     8  ADMIN
$

== cksum (t=108.3 s)
$ cat notes
     1  BPATCH
     2  batch
     3  basename
     4  at
     5  ASA
     6  AS
     7  AR
     8  ADMIN
$ cksum notes
3254500401 97 notes
$

== cksum of notes equals the host's checksum of the expected bytes (t=108.3 s)
$ cat notes
     1  BPATCH
     2  batch
     3  basename
     4  at
     5  ASA
     6  AS
     7  AR
     8  ADMIN
$ cksum notes
3254500401 97 notes
$

== ls (t=109.4 s)
$ cat notes
     1  BPATCH
     2  batch
     3  basename
     4  at
     5  ASA
     6  AS
     7  AR
     8  ADMIN
$ cksum notes
3254500401 97 notes
$ ls
notes
$

== no editor temporary file is left behind (t=109.4 s)
$ cat notes
     1  BPATCH
     2  batch
     3  basename
     4  at
     5  ASA
     6  AS
     7  AR
     8  ADMIN
$ cksum notes
3254500401 97 notes
$ ls
notes
$