How to Quit Vim Without Saving? [Easy Ways]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

To quit Vim without saving, follow the below syntax:

  1. To quit without saving type :q! and press the ENTER button.
  2. To quit without saving and make Vim return non-zero error type :cq from the normal mode and press the ENTER button.
  3. 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:Initial state. 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:After deleting last line. I opened the file on the Vim editor again. The below image shows that no changes were saved:Changes are not 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:Initial state. Now I will delete the last line. Then, I will type :wq and press the ENTER button:After line delete. The image shows that the last line has been deleted successfully. After that, I will open the file again in the Vim editor:File after reopening.

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:Initial state. Now, I will delete the last line in the file:After deleting delete. 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 :.

4.5/5 - (13 votes)
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

3 thoughts on “How to Quit Vim Without Saving? [Easy Ways]”

Leave a Comment