Note: These commands have a colon before them:
When you are finished editing the document, you are ready to exit VI when you return to the shell, you can use one of the following methods.
In command mode, double-press two capital Letter Z, if the currently edited document has been modified, VI saves the document and exits, returns to the shell, and if the currently edited document has not been modified, VI exits directly and returns to the shell.
In the last line mode, enter the command
: W
VI saves the currently edited document, but does not exit, but continues to wait for the user to enter the command. When you use the W command, you can then give the edit document a new document name.
[Example 6]
: w NewFile
At this point VI will save the contents of the current document to the specified NewFile, while the original document remains unchanged. If NewFile is an existing document, VI gives a hint in the status line of the display window:
File exists (use!) To override)
At this point, if the user really wants to replace the contents of NewFile with the current contents of the document, you can use the command
: w! NewFile
Otherwise, you can select a different document name to save the current document.
In the last line mode, enter the command
: Q
System Exit VI returns to the shell. If the edit document is not saved when you exit VI with this command, VI displays the following information in the last line of the display window:
No write since last change (use!) To overrides)
Prompts the user that the document has been modified without saving, and then VI does not exit, continuing to wait for the user command. If the user does not want to save the modified document and want to forcibly exit VI, you can use the command
: q!
VI Discard the changes and return directly to the shell.
In the last line mode, enter the command
: Wq
VI will save the document first and then Exit VI to return to the shell.
In the last line mode, enter the command
: X
The function of this command is the same as the ZZ command in command mode.
VIM Exit command