The aim of this week is
An editor is application software, in which you create and modify documents. In a text editor you create and modify text documents. Not only plain text but also program source code or HTML and other mark-up source and many other files are text in its wider sense as human readable and editable files.
There are a large number of terminal based text editors in Unix. They fall in to four main categories according to their user interface. In a GUI based editors the seperatation between text input and and control commands like font, alignment, etc. are clearly seperated through the tools available in the GUI. For text input there is a seperate area. Once you click on it, the editor knows where the text input that follows should flow. In the terminal you dont have this clear seperation. One has to invent a method of seperating control input from the text input.
In terminal based text editors there are basically four ways of achieving this:
Here simple keystrokes are used as control commands. To diffentiate them from text input, you change the mode of the editor. This is not much different from a paint program: if you select the brush, dragging mouse will paint something – if you’ve selected the eraser, the same movement will erase!
All control commands are entered by Ctrl or Alt key combinations.
Also uses the Ctrl key to enter control commands. But their arrangment is different from Emacs. Joe follows the pioneering wordprocessor WordStar.
Designed specifically for the PC keyboard making use of its special keys for movement like the arrow keys, page-up/down, etc. These editors follow the arrangement which one finds in the Windows Notepad or in the pine mail reader. These editors are the most natural for the Windows user. They use only a few control keys, which are always displayed at the bottom of the screen.
We are going to look at the first category only, the ed-vi line. The main reason for chosing this is that their control keys have a language of their own, which appear in many other places in Unix. Added to that vi is found in almost any Unix variation.
ed was one of the first end-user programs hosted on Unix and has been there ever since. Due to its simplicity the programm is so small, only 47 kB in todays x86 Linux, that it doesn’t cost any space.
The interface is archaic in the sense that ed is a line editor - an editor in which each editing command applies to one or more complete lines of text designated by the user. Let us illustrate this with an example. This is how you create a file in ed:
ed starts with its own buffer, if no file name is mentioned. In the example the command ’a’ (append) instructed the editor to interprets the next keystrokes as text. One says that the editor changed from the command mode to input mode. To return to command mode, one needs to enter a line with ’.’ as its only content. (Which is not written to the buffer.)
The command ’w file’ (write) writes the buffer to the file. Its output says that ’poem’ is 263 bytes long, which you can verify by counting characters or with ’wc’ (word count):
Let us say, now we want to change the word ’little’ somewhere in the first stanza to ’small’. We don’t remember the exact line, so we need to print the full stanza, then change to the correct line and to do a search and replace. Finally take a quick look at the line, save the file and quit.
As we know ’a’ appends text. Let’s say, we want to add the chorus between to two stanzas. This is how it would go:
The vi, which now has cult status, is a natural extension of ed taking it to from its single line operatio to full screen. In ed the command ’a’ switched it from the command mode to the input mode. vi is similar. In addition to ’a’ (append after the cursor) there are a couple of more commands to switch to input mode. For example ’i’ (inserts at the cursor) does exactly what it says.
In ed ’.’ returned the editor to command mode – in vi it is the ’Esc’ key.
Since Vi is a screen editor, it needs more keys to move the cursor around. The standard is h (left), j (down), k (up) and l (right). You’ll realize that you operate those keys with the the three pointing fingers of your right hand! In todays PC keyboards the arrow keys react as expected. But there are a whole lot of movements which you can not get from those special keys.
In addition there is a series of delete commands: ’x’ (delete character under the cursor), ’dd’ (delete line).
The command ’u’ (undo) does what it says.
Right now, it is more important to understand the philosophy of vi. For that we limit our command vocabulary to: a, i, Esc, h, j, k, l, x, X, dd and u. You can practice them in this week’s assignment.
(see Assignment Week 6 in Moodle)