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 ...

Get Learning the vi Editor, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.