Compared to Vi, Vim is more compatible. Vim, one of the best terminal-based editors, has many exciting features. Launching and closing Vim is very simple. This article examines how you can exit out of Vim under the Unix and Linux systems.
How to Open Vim Tutor
Vimtutor is useful for new Linux and Unix developers or sysadmins. You can quickly start the Vimtutor in macOS, Linux/Unix systems by running the following command:
vimtutor
By default, vimtutor starts in English but can also be started in other languages through other commands.
vimtutor fr ## French ##
vimtutor es ## Spanish ##
You can also see all installed languages in Vimtutor by running the following ls command:
Vim Modes
Vim has seven basic modes, and there are seven additional modes. We consider these different modes as variants of basic modes. You can read the document by running:h Vim-mode to know this better. Knowing all the modes is not so important. However, there are three such modes that you should know while using it: command mode, insert mode, and normal mode.
We are in its normal mode while starting the Vim/Vi text editor. This mode allows you to navigate the file according to your needs and use the Vim command. Use the “I” key to enter insert mode here to edit the text. This way, you can edit files, such as configurations or programs. Using the Esc key again will take you back to normal mode.
How to Open a file in Vim
You can start Vim/Vi by typing the following command:
vim file
vim [options] filename
How to Exit Out of Vim Editor
There are several methods or conditions to quit the Vim editor. Let’s discuss some of them.
Condition 1: Save and Quit the Editor Command in Vim
There is a specific procedure to save a file in Vi/Vim and quit the editor.
First, open the Vi/Vim file type and run the following command with your filename:
Press the Esc key to save a file in Vi/Vim and type :w and press enter.
Your file is saved in Vi/Vim. Now, you can quit the Vi/Vim by pressing the Esc key. After that, type any of them according to your condition.
:qa - > to quit all open files
Once you decide and type any of the previous commands, press Enter key to quit the Vim editor, and your editor will be closed.
NOTE: The bottom left of the terminal will show the next keystroke when you press “:”. After that, press : (colon) and note that the cursor will appear back at the left-side bottom corner of the screen near the colon prompt.
Condition 2: After Making Any Changes and Saving the File, You Can Quit the Vim Editor
When in insert or append mode, press Esc first. Once this command has been run, press Enter to submit it.
Or
:x!
Or
:wq
This will save all the changes made in the document to the file and quit the editor.
Condition 3: Exit Vim Editor, Without Changes in File
If you do not want to save any changes in the file, press the Esc key as usual. After that, type :q! and press the Enter key. This will exit Vim, saving no changes.
Other Methods to Quit Vim:
By the way, the previous commands are enough to exit the Vim editor. But, like we had mentioned earlier, Vim Editor is famous because of its shortcuts. So, now we will talk about some of its shortcut keys, through which it will be easier for you to exit the Vim editor.
Condition 1: When you want to quit the Vim editor without saving the file – > Esc + Shift ZQ
Condition 2: After saving the file and then quit the editor – > Esc + Shift ZZ
Condition 3: To quit all open files – > Esc + :qa + Enter
Condition 4: Just save and exit the file – > Esc + 😡 + Enter
It depends on which method you want to adopt to quit the Vim editor.
Troubleshooting While Exiting Vim
Condition 1: If You Don’t Want to Enter the Command-Line Mode
You can press ZQ or ZZ to quit Vim in normal mode with no problem. The ZZ and ZQ characters are the same as 😡 and :q!.
Here, ZZ and ZQ are normal mode commands, where 😡 and :q! are Exit commands.
Condition 2: If You Lost and Want to Force Quit
When trying to quit Vim, you can see the previous information by pressing Control + C in Linux, Ctrl + Alt + Delete in Windows, and force quit in macOS.
The next time you try to open the same file again, review this section (here, I’m using a file named foo.txt as an example):
If you try to open the same file next time, there is nothing to worry about if you see the following image. Here, the Vim Apk helps you recover the valuable changes made that you may have lost.
You can see that a file with the extension of .swp is found by checking the directory. This is a swap file.
After pressing R, you will see that your changes have been reflected after pressing Enter. You can delete the .swp file once you have completed the recovery process of the file. When you remove it, you don’t see the previous error.
Condition 3: When “:wq” Failed
It’s not a big deal to fail “:wq” whenever the filename is missing or read-only. So, when the file is read-only, Vim does not prevent us from editing and opening the file. Maybe even then, the :wq! will not work In this situation. You can finish all the changes through :q, and with the help of sudo, you can open the same file in the prefix and make the changes again.
Conclusion
This article covered some basics on how to exit the Vim editor in different conditions through various methods. There are also several problems while exiting from Vim, due to which you cannot exit from Vim easily. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.