Vim is known as an incredibly powerful text editor but is equally well-known for its steep learning curve. If you frequently work on the command line, it’s worth learning at least some of the basic functionality. Unfortunately, if you don’t take the time to learn it, even something as simple as exiting Vim can be difficult.
Normal keyboard shortcuts won’t help you here. Nothing you’ve learned in your entire computing life will help you, in fact. Vim is very much its own creature, and even to exit it, you’re going to have to learn how to do things the Vim way.
Accidentally Launch Vim? Here’s How to Get Out Fast
If you meant to run another command but accidentally launched Vim instead, exiting is fairly simple. Vim launches in what it calls “Normal mode,” which means you’re not actually editing the file. Anything you type will be interpreted as a command.
First, you’ll need to type the
: key, followed by the letter q to quit. To get out of Vim, type the following:
If you get an error that says “No write since last change (add ! to override),” you accidentally entered text or changed a file. Type the following instead:
Stuck in Insert Mode?
You might have tried to quit by typing a few different letters and accidentally ended up in Vim’s Insert Mode. This is where you actually type new text into a file. Once you’re in this mode, you may not know how to get out. In this case, the above tips won’t help you.
Fortunately, the key to getting out of Insert Mode is simple. Just hit the Escape key. This will return you to Normal Mode and let you use the above instructions to quit the editor.
How to Quit without Saving
It’s possible you launched Vim on purpose, started to edit a file, and now you’re in over your head. If you’ve made changes to the file and don’t want to keep them, it’s simple to quit without saving.
We already mentioned one option for quitting without saving above:
If you’ve opened multiple files and want to quit all of them at once without saving, this is easy too. Just type the following:
How to Save Your Files and Exit
Maybe you’ve made changes and want to keep them. Fortunately, this is just as easy as quitting without saving. You just need to add
w to your command. Try the following:
If you prefer shorter commands, the following will also save and quit:
Other Methods for Exiting Vim
There are a few other ways to quickly quit Vim. First, hit the Escape key to make sure you’re not in insert mode.
Holding down the Shift key and hitting
ZZ will save and quit.
Holding down Shift and hitting
ZQ will quit without saving.
Want to Learn How to Use Vim?
As we mentioned at the top of the article, Vim is powerful and well worth knowing. Even if it seems intimidating at first, knowing the editor can be very useful down the road. That said, you may not be sure where to start.
Fortunately, we can help you in your journey toward learning and eventually mastering Vim. You can start with our beginner’s guide to using Vim. This is the first in a multi-part series that can take you a long way toward getting the most out of the Vim editor.
Our latest tutorials delivered straight to your inbox