To quit Vim without saving, follow the below syntax:
- To quit without saving type
:q!
and press the ENTER button. - To quit without saving and make Vim return non-zero error type
:cq
from the normal mode and press the ENTER button. - To write and quit type
:wq
and press the ENTER button.
In this article, I will demonstrate these approaches to quit vim without saving.
1. How to Quit Vi/Vim Editor Without Saving Changes?
To quit vim without saving, type :q!
and press the ENTER button from the normal mode. Here, I have opened a file in Vim: Now, I will delete the last line from the insert mode (for switching to the insert mode, press the i key). After that, will type :q!
and press the ENTER button to exit Vim without saving the file: I opened the file on the Vim editor again. The below image shows that no changes were saved:
Note: Type :cq
from the normal mode and press the ENTER button to quit without saving and make Vim return non-zero error.
2. How to Save Changes in Vim?
To save changes in Vim, type :wq
and press the ENTER button from the normal mode. In this section, I will open a file in the Vim editor: Now I will delete the last line. Then, I will type :wq
and press the ENTER button: The image shows that the last line has been deleted successfully. After that, I will open the file again in the Vim editor:
Note: Alternatively you can use the command :x
. It will only save if there is any change in the file. You can also use :wq!
to write and quit even if the file has no write permission.
3. How to Save File in Vi / Vim Without Exiting?
To save a file in Vim without exiting, type :w
from the normal mode and press the ENTER button. Here, I have opened a file in Vim: Now, I will delete the last line in the file: After that, I will type :w
from the normal mode and press the ENTER button. The below image shows that the file save is successful:
Quit Vi/Vim Editor
Here are some exceptional commands used for quitting the Vim editor:
:qa
– Quits all.:qa!
– Quits all open files without saving changes.ZQ (Shift + ZQ)
– Quits without saving changes.:exit
– Writes and exits.
Conclusion
I have explored different approaches to quit Vim without saving. This is a helpful tool to recover unintentional changes. Do not forget to put your valuable thoughts in the comment box.
People Also Ask
How to exit Vim text editor?
To exit Vim text editor, type :wq
from the normal mode and press the ENTER button. It will save the file and exit the Vim editor.
How do I force quit Vim?
To force quit Vim, type :q!
from the normal mode and press the ENTER button.
Does Vim automatically save?
No, auto-save is disabled by default in Vim.
What is command mode in Vim?
Command mode in Vim is a mode where you can execute a command preceded by a colon :
.
FUNDAMENTALS A Complete Guide for Beginners
It was very helpful…keep it up
Agreed!
thanks for ur appreciation