Vim or Vi is a command-line text editor which is mainly managed with the commands and key shortcuts. After making changes in a file you may want to quit or exit from Vim. In this tutorial, we will learn how to quit Vim or Vi without saving changes.
Open and Make Changes In Vim
First, we will open a file named sample.txt with the Vim like below.
$ vim sample.txt
Quit Without Saving Changes with :quit! Command
After making some changes we may want to quit. But there is a question we should answer the question “Will we save the changes?”. For this case, we will not save the changes and exit. We will follow these steps.
- Press the ESC key in order to change normal mode.
- Type :quit!
But keep in mind that all changes will be lost forever. You can not revert back in any way.
Quit Without Saving Changes with :q! Command
Vi/Vim provides shortcuts for commands to type faster and shorter. The :quit!
command can be shortened like :q!
.
Quit GVim Without Saving
The GVim is a graphical tool that implements the Vi/Vim. We can quit from GVim via the File
-> Exit
menu without saving the file.
The “Save changes to ‘cities2.txt’?” question asked to save the file. Just click to the No
to prevent save and quit without saving.
Kill Vi/Vim To Quit Without Saving Changes
Even Vi/Vim provides different commands and ways to quit without saving files there are alternative and unconventional ways to quit Vi/Vim. The pkill
command can be used to quit Vi/Vim by killing the Vi/Vim process which prevents saving files.
$ pkill vim