VI Tutorial - Lesson 3 - Exiting VI

When you have finished your editing, you need to inform VI that you wish to "quit" the editor and return to your shell. This is done by typing ":q" (quit). VI will respond by updating the file information line with the name of your file in quotes followed by the current number of lines and characters. After exiting the editor, the UNIX prompt will again appear on your screen.

3.1 Exiting the Editor -- Retaining Changes

VI requires that the buffer be empty of newly edited material when you type ":q". Thus if you have made any alterations to the file since the last time you typed ":w", VI will not know how you want these changes handled. The editor will print the statement:

 

     No write since last change (:quit! overrides)

at the bottom of the screen. If you decide you want to retain these changes, you must type ":w" before issuing a new ":q". Most system users get in the habit of combining these two commands into a single command ":wq" (write and quit).

Diagram of opening and editing a file.

The VI editor allows a user to repeatedly make alterations to a file. This is very handy when you are preparing a term paper, computer program, or report because each time you reread a section you conjure up better ways to word your thoughts.

Consider the simple file in this example, The file has 3 lines when opened and is then edited as follows:

 

  1. The user corrects an error on line 2 that existed from a previous editing session. When the ":w" command is issued, all 3 lines on the disk file will be overwritten with the 3 lines as they exist on the buffer.

     

  2. Next, the user decides to delete the 2nd line. When the ":w" command is issued all 3 lines on the disk file will be overwritten with the 2 lines as they exist on the buffer.

     

  3. Later, 2 new lines are inserted into the buffer copy between lines 1 and 2. When the ":wq" command is issued, the 2 lines on the disk file will be overwritten with the 4 lines as they exist on the buffer. The contents of the buffer are discarded with the termination of the editing session.

3.2 Exiting the Editor--Discarding Changes

Sometimes when working on a file, it is desirable to leave the editor without saving the modifications. This is accomplished by typing ":quit!". You might use this command if you started to edit a file and do not like the way the changes are shaping up. When you use this command, VI will immediately discard all alterations made to your file since the last ":w". If you have not used a ":w" since opening the file, all changes since the beginning of the editing session will be abandoned.

The action of the ":quit!" command is illustrated below. Initially when the file was opened, it contained 3 lines. Some form of editing was done on these lines and the work was saved with the ":w" command. Additional material was keyed in, but the decision was made to discard the newer additions. When the ":quit!" command is issued, all text back to the ":w" is disposed of.

Diagram of editing a file and then using the ":quit!" command to exit without saving your changes.

BE CAREFUL!!! When you use the ":quit!" command, the editor believes you know what you are doing and will immediately follow your instructions. You will not have a second chance to retrieve your work once the buffer is closed and the file is discarded; so, key in this command with utmost caution.

3.3 Exiting VI--Keeping Changes and Original Text

There may be a time when you are editing a file and can not decide if you want to keep the current disk copy or retain the most recent changes. VI offers you a way to "have your cake and eat it too". This is done by writing the current contents of the buffer into a new file. For example, if the original file is named apples, you could write the material from the buffer into a file named oranges then exit the editor discarding the buffer contents with ":quit". The file oranges contains the altered text and the file apples contains text as of the last issued ":w".

Another diagram of opening and editing a file.

 

The sample file, apples, has 3 lines when opened and is then edited as follows:

 

  1. The user corrects an error on line 2 that existed from a previous editing session. When the ":w" command is issued, all 3 lines on the disk file will be overwritten with the 3 lines as they exist on the buffer.

     

  2. Next, the user deletes the 2nd line, but can not decide to keep or discard the change, therefore; writes the contents of the buffer to a new file, oranges with ":w oranges". The buffer is then discarded with ":quit!" and the existing file, apples remains on the disk as it existed after the last ":w" was issued in step 1.

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