6 Text editors

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:

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.

6.1 The original text editor ’ed’

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 

Great fleas have little fleas 
  upon their backs to bite ’em, 
And little fleas have lesser fleas, 
  and so ad infinitum. 
 
And the great fleas themselves, in turn, 
  have greater fleas to go on; 
While these again have greate still, 
  and greater still, and so on. 

w poem 
263 
q

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):

$ wc poem 
  9  46 263 poem

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.

$ ed poem 
263 
1,4p 
Great fleas have little fleas 
  upon their backs to bite ’em, 
And little fleas have lesser fleas, 
  and so ad infinitum. 
3p 
And little fleas have lesser fleas, 
s/little/small/ 

And small fleas have lesser fleas, 

262 
q

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:

$ ed poem 
262 
5p 
          < empty line 

[Chorus] 
          < empty line 

5,7p 
          < empty line 
[Chorus] 
          < empty line 

272

6.2 ’Vi’, the visual editor

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.

6.3 Assignment for the week

(see Assignment Week 6 in Moodle)

Summary