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.
Save the contents of the buffer (write the buffer to the file on disk) by typing:
:w |
followed by Return.
:wq |
followed by Return. Alternatively, type ZZ.
Note that the command ZZ is neither preceded by a colon nor followed by Return.
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! |