OpenWindows Advanced User's Guide

6.3 Ending a Session

When you edit a file in vi, your changes are not made directly to the file. Instead, they are applied to a copy of the file that vi creates in a temporary memory space called the buffer. The permanent disk copy of the file is modified only when you write (save) the contents of the buffer.

This arrangement has its good and bad points. On the one hand, it means that you can quit a file and discard all the changes that you have made during an editing session, leaving the disk copy intact. On the other hand, you could lose the (unsaved) contents of the work buffer if the system crashes. (People on remote terminals connected by phone lines are especially vulnerable to unplanned interruptions.)

The best policy is to save your work frequently, especially when making substantive changes.


Caution - Caution -

Although it's possible to run multiple, simultaneous vi sessions on one file, it is not a good idea. Great confusion could result when you try to determine which changes have been written to the file and which changes have been overwritten from a simultaneous session.


6.3.1 Saving Changes and Quitting vi

vi is rich in more or less synonymous commands that control saving the buffer contents to a file and quitting vi. These commands give you the option of saving, saving-and-quitting, or quitting-without-saving.

6.3.1.1 Saving

Save the contents of the buffer (write the buffer to the file on disk) by typing:

:w

followed by Return.

6.3.1.2 Saving and Quitting

Save and quit by typing:

:wq

followed by Return. Alternatively, type ZZ.

Note that the command ZZ is neither preceded by a colon nor followed by Return.

6.3.1.3 Quitting Without Saving

When you've made no changes to a file and simply want to quit, type:

:q

followed by Return. If you have made changes, vi will not let you quit with :q. Instead, it will display the message, No write since last change (:quit! overrides).

If you do not want to save your changes, type:

:q!

followed by Return.