“How to exit Vim?” “How to quit Vim?” “How do you exit Vi editor?” “How to save and quit Vim?”
These are some of the most googled questions about the Vim editor. Vim, one of the best terminal based editors, is known for its powerful features. Its ardent users swear by it, but it leaves new users baffled because of its “unusual shortcuts.” This even leaves them wondering how to exit from the Vim editor.
In this article, I’ll show you several ways to exit Vim. We’ll also see some interesting fun facts about exiting Vim.
How to exit Vim?
As you have already seen in advanced Vim tips, there are multiple ways to quit Vim. Let me list them one by one, along with the procedure.
Step 1: Enter the command mode by pressing Esc key
Press Esc key: This is very important, because you must exit the edit mode first before typing the exit command(s).
Step 2: Quit vim using commands
Next, you can type one of the following commands to exit Vim in different mode:
- :q (yes, the colon is included in the command) – This will quit the editor. It may prompt you if you are trying to exit Vim and there are unsaved changes.
- :q! – Quit Vim without saving the data file (all unsaved changes are discarded).
- :wq – Save the file and exit Vim.
Step 3: Press enter key
Once you have made your choice of the exit command, press enter to finally quit Vim and close the editor (but not the terminal).
Do note that when you press “:” the editor will have the next keystrokes displayed at the bottom left of the terminal.
Other ways to exit Vim
Normally, you should remember the above three commands and you should be good with exiting Vim. But as I said earlier, there are more shortcuts to quit Vim. These are the following:
- Esc + :x + Enter (Save and exit)
- Esc + :qa + Enter (Quit all open files)
- Esc + Shift ZZ (Save and exit)
- Esc + Shift ZQ (Exit without saving)
Now it is really up to you to choose how you want to quit the Vim editor. If you ask me, the first method is what you should opt for.
You save (without exiting) with :w command and then you can use :wq to save and quit (w for save and q for quit). It is easier to remember q for quit.
Facts and fun about exiting Vim
If you are feeling a little low because you didn’t know how to quit Vim, don’t, because you are not the only one. According to Stack Overflow, over a million developers worldwide searched for how to exit Vim.
In fact, exiting Vim has become a talking point in the popular culture. Take this tweet, for example. This is one of the most popular jibes at the “complexity” of exiting Vim:
The widespread difficulty is somewhat surprising, because if you run Vim you’ll see the information about how to exit it at the splash screen:
But then, obvious things are not that obvious at times. It’s funny that we are so accustomed to Ctrl+S, Ctrl+X shortcuts that it baffles us when we don’t find the same “standard” shortcuts elsewhere.
Vim is an excellent editor. If you can master its features, there will be nothing else like it. SysAdmins particularly spend a lot of time on the command line. Mastering Vim can really be beneficial. At least, you should master the basic vim commands.
I hope this tutorial helped you to save and exit from Vim. Do share your views and thoughts on it.