In command mode, double-press two capital Letter Z, if the currently edited file has been modified, then VI save the file and exit, return to the shell; if the currently edited file has not been modified, VI exits directly and returns to the shell.
In the last line mode, enter the command
: W
VI saves the current edit file, 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 file a new file name.
[Example 6]
: w NewFile
At this point VI will save the contents of the current file to the specified NewFile, while the original file remains unchanged. If NewFile is an existing file, 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 file, you can use the command
: w! NewFile
Otherwise, you can select a different file name to save the current file.
In the last line mode, enter the command
: Q
System Exit VI returns to the shell. If the edit file 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 file 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 file 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 file first and then Exit VI to return to the shell.
In the last line mode, enter the command
: X
The command functions the same as the ZZ command in command mode
Vim Exit command (save, discard Save)