home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


1.2 Quitting Without Saving Edits

When you are first learning vi , especially if you are an intrepid experimenter, there are two other ex commands that are handy for getting out of any mess that you might create.

What if you want to wipe out all of the edits you have made in a session and then return to the original file? The command:

:e!
 [RETURN]

returns you to the last saved version of the file, so you can start over.

Suppose, however, that you want to wipe out your edits and then just quit vi ? The command:

:q!
 [RETURN]

quits the file you're editing and returns you to the UNIX prompt. With both of these commands, you lose all edits made in the buffer since the last time you saved the file. vi normally won't let you throw away your edits. The exclamation point added to the :e or :q command causes vi to override this prohibition, performing the operation even though the buffer has been modified.

1.2.1 Problems Saving Files

  • You try to write your file, but you get one of the following messages:

    File exists
    File file
    
     exists - use w!
    [Existing file]
    File is read only

    Type :w! file to overwrite the existing file, or type :w newfile to save the edited version in a new file.

  • You want to write a file, but you don't have write permission for it. You get the message "Permission denied."

    Use :w newfile to write out the buffer into a new file. If you have write permission for the directory, you can use mv to replace the original version with your copy of it. If you don't have write permission for the directory, type :w pathname/file to write out the buffer to a directory in which you do have write permission (such as your home directory, or /tmp ).

  • You try to write your file, but you get a message telling you that the file system is full.

    Type :!rm junkfile to delete a (large) unneeded file and free some space. (Starting an ex command with an exclamation point gives you access to UNIX.)

    Or type :!df to see whether there's any space on another file system. If there is, choose a directory on that file system and write your file to it with :w pathname . (df is the UNIX command to check a d isk's f ree space.)

  • The system puts you into open mode and tells you that the file system is full.

    The disk with vi 's temporary files is filled up. Type :!ls /tmp to see whether there are any files you can remove to gain some disk space.[3 ] If there are, create a temporary UNIX shell from which you can remove files or issue other UNIX commands. You can create a shell by typing :sh ; type [CTRL-D] or exit to terminate the shell and return to vi . (On most UNIX systems, when using a job-control shell, you can simply type [CTRL-Z] to suspend vi and return to the UNIX prompt; type fg to return to vi .) Once you've freed up some space, write your file with :w! .

    [3] Your vi may keep its temporary files in /usr/tmp , /var/tmp , or your current directory; you may need to poke around a bit to figure out where exactly you've run out of room.

  • You try to write your file, but you get a message telling you that your disk quota has been reached.

    Try to force the system to save your buffer with the ex command :pre (short for :preserve ). If that doesn't work, look for some files to remove. Use :sh (or [CTRL-Z] if you are using a job-control system) to move out of vi and remove files. Use [CTRL-D] (or fg ) to return to vi when you're done. Then write your file with :w! .

1.2.2 Exercises

The only way to learn vi is to practice. You now know enough to create a new file and to return to the UNIX prompt. Create a file called practice , insert some text, and then save and quit the file.

Open a file called practice in the current directory: vi practice
Insert text: i any text you like
Return to command mode: [ESC]
Quit vi , saving edits: ZZ


Previous: 1.1 Opening and Closing Files Learning the vi Editor Next: 2. Simple Editing
1.1 Opening and Closing Files Book Index 2. Simple Editing

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System