VI Tutorial - Lesson 2 - Entering or Calling VI

2.1 Calling VI

To call the VI editor and begin an editing session, type at the UNIX prompt (%):

 

     %  vi filename [RETURN]

where filename is the name of the file that you want to edit. VI automatically sets aside a temporary buffer as a working space for use during this editing session.

2.2 Editing A New File

If the file you specify when calling VI does not already exist in your current working directory, a new file will be created with the name that you have specified. For example, if you wanted to create a new file called jobjar, you would type:

 

     % vi jobjar [RETURN]

The screen will flicker and clear and you will see:

  ← CURSOR
-
-
-
-
-
"jobjar" [new file] ← file information line

 

When the VI editor creates a new file as in the previous example, the cursor will be placed on the screen in the home position (upper left-hand corner). The cursor on most terminals is a rectangle that completely covers the character space it is situated upon and reflects your current position within the buffer. Below the cursor along the left side of the screen, is a tilde ( ~ ) character positioned in the first character space on each line. The tilde is used to indicate a void area in the file where there are no characters, not even blank spaces. At the bottom of the screen, the editor displays a file information line which presents the name of the file being edited, in this case jobjar, followed by "[New file]" to indicate that this is a new file and it has never been opened before.

2.3 Editing An Existing File

The VI editor allows a user to modify and add material to an already existing file. This is done by using the name of a current file when calling VI. VI will then fetch a copy of the contents of the specified file from disk storage, leaving the original undisturbed, and place the copy into the buffer. It is here, in the buffer, where VI permits you to do your editing. The idea of editing in the buffer is very beneficial, because if you really botch up your editing job (such as deleting a major section) the original file on the disk remains unaffected.

Upon opening a file, VI will display the first screenful of text starting at the top of the file, place the cursor in the home position, and print the file information line which displays the name of the file along with the number of lines and characters it contains on the last line of the screen. For example, to edit the file sample which has 40 lines and 750 characters, call the VI editor with this command:

 

     % vi sample [RETURN]

The screen will flicker and clear. Then you will see:

This is some sample text.
This is some sample text.
This is some sample text.
This is some sample text.
This is some sample text.
This is some sample text.
"sample" 40 lines, 750 characters

 

As shown in this example the cursor sits in the home position, it completely overlays the first character of the file. When the cursor is moved, this character will be exposed, unchanged. In general, most terminal screens will display only 20 to 24 lines of text. If a file has more text than will fit on one screen, the unviewed material remains in the buffer until needed.

2.4 Saving or Storing A File

As mentioned before, when the VI editor is invoked a temporary working area is created called a buffer. This buffer or work space is only available for your use while you are using the editor. It is into this buffer that VI either places a duplicate copy of the file as it exists on the disk or creates a new file. When you exit the editor, this work area is discarded. Disk storage, on the other hand, can be thought of as a permanent storehouse for files until discarded by using the UNIX remove command.

Few things are more upsetting than to spend many hours keying information into a file, simply to have a careless keystroke close the buffer and destroy your work. Material you have keyed into the buffer file is only retained for future access when the file is "saved" on the disk. Material not "saved" on the disk is discarded when exiting VI and the buffer is closed. The next figure depicts this relationship.

Saving a file

To avoid the frustration of rekeying material, you should periodically "save" the work done to that point by "writing" the new material or alteration to the permanent disk storage. This is done by typing ":w" (write). As soon as you type ":" the cursor hops to the bottom of the screen and it is here that the complete ":w" command is echoed. NOTE: It is difficult in simple drawings to show the cursor and the command in both the position where the command is issued plus the location at the bottom of the screen where it is echoed; therefore for brevity, in the following examples you will see the command presented at the point in the text where it is issued.

When the user instructs the editor to save material with ":w", a copy of all material in the buffer is transported to the disk, overwriting the previous contents of the disk file. As a general rule, it is suggested that you "write" material to the disk every 15 to 20 minutes, or after you have keyed in enough information or corrections that it would irritate you if you needed to rekey it in all over again.

On the next page is an example of a file containing 3 lines of original text and the user keys in 2 additional lines followed with ":w". When the ":w" command is issued, the disk file copy will be overwritten with the 5 lines as they exist in the temporary work area. This material is referenced as section "A" in the next example.

What happens to your file if the system crashes or you make a dumb mistake in the midst of an editing session? Well, if your file looked like the one below when disaster struck, all material in section "A" would be safe because the ":w" would have caused everything to that point to be written to the disk. The material in section "B" might be completely or partially destroyed because it only exists on the temporary buffer.

Writing to disk

An editing session can last for minutes or hours but the user can be assured that once material is written to the disk, the storage is permanent and the material will remain there until needed again.


previous | MENU | next

Last modified: 2009/09/01 09:46:26.251000 GMT-4 by christopher.n.deckard.1
Created: 2007/11/06 14:24:30.993000 US/Eastern by brian.r.brinegar.1.

Categories

Search

Type in a few keywords describing what information you are looking for in the text box below.

Admin Options: Edit this Document