Introductory vi

The vi editor is an editor that is available with all standard Unix and Linux systems. It is a screen-oriented editor that is different from most other editors you might have used. It does not use a mouse and it is built for efficiency, both of which mean that vi is not very user friendly.

Vi was created long before there were computer mice (back when the earth's crust first began to cool). Since you don't use a mouse, using vi is at first awkward. But, vi is a very powerful tool that is much more efficient once you have learned to use it.

Starting the vi Editor
To edit a file that will have the name "hello.cpp", you issue the command
        vi hello.cpp
If the file hello.cpp does not exist, you will get a blank screen except for a ~ in the first column of each line. The ~ is a symbol for an empty line (which is different from a blank line). The cursor will be in the upper left hand corner of the screen after the ~ on the first line.

Command Mode v. Insert Mode
There are two major modes in vi, "command mode" and "text entry mode", also called "insert mode". The editor starts in command mode. You must issue a text entry command to get into text entry mode. Therefore, you must learn some of the fundamental commands such as "i"-insert, "x"-delete, and "dd"-delete described below. However, first you have to learn to start vi.

Entering insert mode
To start typing text into the file, type i for insert. Note that this "i" command is not displayed on the screen. That is, there is no change on the screen. In other words, there is no way to look at the screen and tell if you are in insert or command mode (not very friendly, is it).

Start inserting by typing the first line of the "hello.cpp" program from Lab 1. If the characters you type are displayed, then you are in insert mode. Also, note there is no "wrap-around" in this editor; you must press the return key at the end of each line.

If you make any mistakes, do not try to correct them yet, since things might not work the way that you might want! (Remember, this is NOT a friendly editor.)

Exiting insert mode
Now exit insert mode by pressing the Esc key. Again, you will notice there is no change in the screen. However, what you type next will be interpreted as commands and not added to the file (since you are now in command mode). If you are in command mode and you press Esc, terminal might beep at you (do not worry, this does not mean you made a mistake). This is the only way to determine which mode you are in. Press Esc several times. If it beeps, you are in command mode; if it does not beep you were in input mode, but now are in command mode.

Moving the cursor
The arrow keys on the keyboard can be used to move the cursor to specific positions in the text. However, in older systems, you must be in command mode to do this!

Get into command mode now (press Esc), and try moving the cursor around. It will only move over text and not past the end of the line. On a PC you may need to make sure the "Num Lock" is off. The cursor keys do not move the cursor in insert mode, so don't use them in insert mode!

For an experiment, move the cursor to the end of the “cout” line, and get into insert mode (type i). Then press the right-arrow key. What happened? This is not what you wanted, so let's learn how to delete characters.

Deleting Characters
To delete a character, get into command mode, then place the cursor over (on top of) the character to be deleted. Now press x and the character under the cursor will disappear and no "x" is displayed.

Try x-deleting some of the characters that you already have typed. Note that you may not use "backspace" in command mode, only in insert mode.

Deleting Lines
To delete an entire line, get into command mode. Then position the cursor anywhere on the line you want to delete, and type dd .

Refreshing the screen
Sometimes (in older systems) the text on the screen may get messy from inserts and deletes. The best way to clean up these errors and have the screen accurately reflect the file's contents is to refresh the screen.

You do this by pressing Ctrl-L (the "control" key and the "L" key simultaneously). Press Ctrl-L now, and note what happens on the screen.

Exiting the editor
To save changes and exit the editor type ZZ (Shift-Z Shift-Z). Notice this command is capitalized. No backup file is kept, so if you do not like these new changes, tough.

To exit the editor but NOT save the changes you made, get to command mode and then type :q! <return> (that is, press , type colon, "q", an exclamation point, and then press RETURN).

You might use this if you have really messed up the file in vi. This will keep the file as it was before you started editing it.


command function
i enter insert mode
Esc enter command mode
x delete this character
dd delete this line
:q! quit without saving
ZZ save file and exit vi