How to Quit VIM: 10 Ways to Exit VI
VIM is a very powerful command line text editor that is loved by experts and often hated by novices, mostly because it has a fairly steep learning curve that may seem unintuitive to those who aren’t familiar with it. Even something as simple as quitting out of VIM can seem like a challenge to those who aren’t familiar with VI, and often results in mashing Control+C Control+X !q !q enter escape Control+Z until eventually something happens and maybe the process suspends or maybe quits who knows for sure, so just follow that up with a “killall vim” and on you go, back to open the same text file again in nano, right?
OK, that’s not ideal, but we’ve all been there. That’s what we’re looking to avoid from now on, because even if you don’t want to use vi you can at least know how to exit it properly. This tutorial will show you exactly how to quit VIM / VI properly, actually it will show you multiple different ways to exit VI!
10 Ways How to Quit VIM / VI
This is a question that appears in our comments fairly often on command line articles… how on earth to actually quit out of VIM? It turns out there are literally 10+ ways to quit out of VIM, which is perhaps indicative of why VI baffles so many users. Let’s get to the easiest methods first:
Quit VIM without saving with ZQ
Hit the ESCAPE key, then hit SHIFT + ZQ
This will instantly quit out of VIM without saving, basically the same as the :q! command.
Quickly Quit VIM with Write & Save to File
Hit ESCAPE key, then SHIFT + ZZ
These are the two fastest ways to exit out of VIM for me personally, but everyone has their own opinions here and many prefer to type a command in the more traditional manner.
Quit VIM with :q
Hit the ESCAPE key, then type :q and hit RETURN
To be perfectly clear, hitting the “Escape” key enters into command mode. Then typing :q is literal, as in a colon not a semi-colon, so it would be Shift+; followed by q and hitting the Return key enters the command to quit.
This only works if no changes have been made to the document, so to quit if changes were made you make a slight adjustment and at a bang at the end:
Hit the ESCAPE key, then type :q! and hit RETURN
Quit VIM and write changes with :wq
Hit ESCAPE and type :wq and hit RETURN
This saves (writes) changes to the active file and quits. You can force this by adding a bang if necessary:
Hit ESCAPE and type :wq! followed by the RETURN key
This should help avoid this situation:
We can be a bit more thorough though and cover every possible way to quit VIM (at least that I know of courtesy of the man page, chime in the comments with more ways if there are others that we missed here), which we’ll get to next:
Hit ESCAPE key to enter into command mode first, then use any of the following :
- :q – quit
- :q! – quit without saving, even if modified
- :cq – quit always without writing
- :wq – write / save the current file and exit
- :wq! – write the current file and exit always
- :wq (name) – write to file (name) and exit
- :wq! (name) – write to the file (name) and exit always, even if modified
- ZZ – save the current file if modified, then exit
- ZQ – quit and exit without saving
- Launch a new terminal and type ‘killall vim’ – this is a joke that many will understand, and while not a proper way to quit out of vim it does work
So that’s how to escape VIM, any less confused? Perhaps not, and that’s OK, we’ll generally stick to using nano around here for walkthroughs because it’s more user friendly. And there’s nothing wrong with that, even though I’ve grown more comfortable with VIM over time, I still prefer nano out of ease and perhaps old stubborn habits.
For those interested in learning VIM or at least becoming more comfortable with it, you can always try the vimtutor command, use this excellent online interactive VIM tutorial, and just get more practice by using it often on any terminal. You can even get vim on your iPad or iPhone if you’re really committed. And at least you know how to quit out of vi now, right?