Week 4. Topic "Text editor"

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 is a large number of terminal based text editors in Unix. In GUI based editors the seperatation between text input and and control commands like font, alignment, etc. are built in to the GUI. There is a separate area for text input. The GUI knows that the text input in to the area should flow in to the text. In the terminal you dont have this separation. 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:

  • The original ed, ex and its successors vi

    These editors use plain keystrokes 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!

  • Emacs (Edit MACroS editor) and its derivatives

    All control commands are entered by Ctrl or Alt key combinations.

  • joe (Joe’s Own Editor)

    Also uses the Ctrl key to enter control commands. But their arrangement is different to Emacs. Joe follows the pioneering word processor WordStar.

  • pico, nano

    Designed specifically for the PC keyboard making use of its extended keys for movement like the arrow keys, page-up/down, etc. These editors follow the logic in the Windows Notepad or in the pine mail reader. Not surprisingly, these are the most natural editors 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 choosing this is that their control keys have a language of their own, which appear in many other places in Unix. Added to that vi or its successor Vim (Vi IMproved) is found in almost any Unix variation.

4.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 program is so small, only 47 kB in today's 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 named 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 find the correct line and to do the search and replace, 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, 
w
 
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 
a  [Chorus]
 
          <− empty line 

5,7p
 
          <− empty line  [Chorus] 
          <− empty line 
w
 
272

6.2 ’Vi’, the visual editor

The vi (VIsual editor), which now has cult status, is a natural extension of ed taking it to from its single line operation 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 (insert at the cursor) does exactly what it says.

In ed a . (dot) 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!

There are tons of movement commands. A few useful commands are: 0 (zero) move to the beginning of a line, $ move to the end of the line. In today's PC keyboards the special keys up-and down-arrows, Home and End keys do the same things. But vi brings a whole lot more like w (word), W (full word), b (word back), B (full word back).

/TEXT (slash followed by the search pattern) searches for TEXT. Type n to search further, N to search backwards.

One can run a command multiple times by prepending it with the number of repetitions: Try 5h or 10j.

In addition there is a series of delete commands: x (delete character under the cursor), X (delete character left to the cursor). d (delete) initiates a delete, combined with a movement key. Try d0 and d$. dd deletes a complete line.

The command y (yank) is useful too. It simply copies text in to key buffer. For example yw yanks the word to the right of the course, yy yanks a whole line.

The command p (put) inserts the buffer to the left of the cursor. P puts if on the cursor.

The command u (undo) does what it says in multiple stages.

The : (semicolon) changes Vi to ed/ex mode. Try :w or :q or :r.

And finally there is ZZ to save and quit.

The above subset of commands is sufficient to start working in vi. You can practice them in this week’s assignments.

Last modified: අඟහරුවාදා, 14 දෙසැම්බර් 2021, 12:18 පෙ.ව.