Hi, Community!
This article describes the difference between :wq and :wq!
How to use the :wq/:wq! command
1. Use cd command to enter the path of the object file.
2. Use vi object file to edit the file.
3. The vi has three mode: Command mode, Insert mode, and Last line mode.
Command mode: When you start vi, you enter command mode. The keystroke action in this state is recognized by Vim as a command, not as an input character.
i: Switch to insert mode.
x: Deletes the character where the current cursor is located.
: : Toggles the last line mode to enter commands on the bottom line
Insert mode
Last line mode:
q: Exit the program.
w: Save the file.
ESC: exit the Last line mode.
4. Enter i to enter the insert mode.
5. Edit the file.
6. Enter : to enter last line mode, and enter wq/wq! to save and exit.
The difference between :wq and :wq!
:Wq (Save edit operation and exit): Writing even if the file has not been modified, and updates the modification time of the file.
:Wq! (Save edit and force exit): Mandatory write to file and forced exit, ! for enforcement.
Some files are set to read-only. Generally, they are not modified. However, if you are the owner or root of the file, you can run :wq! to save the file and exit the file. If you do not have permission on the file, it does not force a write.
The difference between :wq and :x
:x Write to the file and exit. Write only when the file is modified, and update the file modification time; otherwise, the file modification time will not be updated.
Both :wq and :x are generally the same, but in terms of programming, there may be a significant impact on editing source files. Because the file is not modified, ":wq" forces the update time of the file to be updated. This will cause make to compile the entire project, thinking that the file has been modified, and then recompile the link to generate the executable. This can have misleading consequences and, of course, unnecessary system resource costs. Therefore, we have to get used to :x to save resources.
The difference between :q and :q!
:q in vim indicates that the user exits. :q! indicates that the file is not saved and exits forcibly.
The differences between :q and :q!" are as follows:
If the file is modified, the :q prompts you whether to exit. Enter y to exit.
If the file is modified, run the :q! command. will force a direct exit, and the file will not be retained.
Thanks!


