Saving and Quitting a File
You can quit working on a file at any time, save your edits, and return to the Unix prompt. The vi command to quit and save edits is ZZ. Note that ZZ is capitalized.
Let’s assume that you do create a file called practice to practice vi commands, and that you type in six lines of text. To save the file, first check that you are in command
mode by pressing ESC , and then enter ZZ. and type :wq to both save your edits and quit. (:wq is equivalent to ZZ.)
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! ENTER
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! ENTER
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.