How to quit vim
How to quit vim
How to Quit VIM: 10 Ways to Exit VI
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:
Every possible way to quit out of VIM
Hit ESCAPE key to enter into command mode first, then use any of the following :
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?
How do I exit Vim?
I am stuck and cannot escape. It says:
But when I type that it simply appears in the object body.
13 Answers 13
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Hit the Esc key to enter «Normal mode». Then you can type : to enter «Command-line mode». A colon ( : ) will appear at the bottom of the screen and you can type in one of the following commands. To execute a command, press the Enter key.
You can also exit Vim directly from «Normal mode» by typing ZZ to save and quit (same as 😡 ) or ZQ to just quit (same as :q! ). (Note that case is important here. ZZ and zz do not mean the same thing.)
Pictures are worth a thousand Unix commands and options:
I draw this to my students each semester and they seem to grasp vi afterwards.
Vi is a finite state machine with only three states.
Upon starting, vi goes into COMMAND mode, where you can type short, few character commands, blindly. You know what you are doing; this isn’t for amateurs.
When you want to actually edit text, you should go to INSERT mode with some one-character command:
Now, answering the question: exiting.
You can exit vi from EX mode:
w and x accept a file name parameter. If you started vi with a filename, you need not give it here again.
At last, the most important: how can you reach EX mode?
From INSERT mode, you need to push ESC, i.e. the Escape button, going to COMMAND mode, and then: to go to EX mode.
If you are unsure, push ESC and that will bring you to command mode.
The robust method is ESC-:-x-Enter which saves your file and quits.
Before you enter a command, hit the Esc key. After you enter it, hit the Return to confirm.
Commands marked with ‘*’ are Vim-only (not implemented in Vi).
Safe-quit (fails if there are unsaved changes):
Prompt-quit (prompts if there are unsaved changes)
Write (save) changes and quit:
Discard changes and quit:
Press Return to confirm the command.
This answer doesn’t reference all Vim write and quit commands and arguments. Indeed, they are referenced in the Vim documentation.
Vim has extensive built-in help, type Esc :help Return to open it.
This answer was inspired by the other one, originally authored by @dirvine and edited by other SO users. I’ve included more information from Vim reference, SO comments and some other sources. Differences for Vi and Vim are reflected too.