How to: Exit vim
Believe it or not, how to exit Vim is one of the most popular questions that Linux and other Unix newcomers ask computer professionals. If you don’t know how to exit Vim or any other vi implementation, then you don’t have anything to feel embarrassed about. It comes with the territory of being new to these operating systems. It’s actually something that even many developers ask when they’re first introduced to Vim and vi, because they’re so used to other environments.
For the purposes of this guide, we’ll assume that you’re already inside Vim or another version of the vi editor. Most of this advice should work whether you started it from the command line with the vi or vim command, and it should even work if you’ve used the busybox vi command. You can launch a command terminal in any of the usual ways to get started, but if you’re looking to close vi then you’re probably already inside of it.
Method 1: Exiting Vim Quickly
If you’ve looked up this guide because you accidentally entered Vim or vi and just want out without saving any work at all, then you should type :q! and push enter. That should take you out of the editor. If it doesn’t, then push the Escape (Esc) key and type :q! followed by enter. You’ll be back at the command line while deleting any changes you might have made. You don’t need to worry about having made any permanent change to any file this way!
Read on to learn more, or if you want to save your work.
Method 2: Vim Save and Quit Techniques
While this is a bit difficult for those who aren’t as familiar with Vim or vi, these editors are modal. This means that there’s an insert mode where you actually type text into a document and a command mode where you issue commands just like if you were at any other command line, though the command structure is very different from what you’re probably accustomed to with bash or something like that. Keep in mind that these commands aren’t the same things as commands on the command line, but only work in Vim or vi.
To switch to the command mode in Vim or vi, push the Esc key. You can also use Ctrl+[ as a shortcut, since the Esc key is far from the letter keys on a modern keyboard. Now you can type :wq and push enter to save what you’ve edited and then quit. The abbreviation means write and quit, so it’s easy to remember :wq as the command. If you’ve made no changes, then typing :q and pushing enter will return.
If you’ve made changes, then the :q command will complain about having unsaved changes. You can loose your changes and just exit the editor with the :q! command. This was the command we previously advised new users to use if they accidentally entered Vim and wanted out. This does dump your changes, though, so you’ll loose anything you were working on.
Should you have started vi without any file name and try to save your changes, then you’ll get an error that reads No such file or directory, since you didn’t specify a name. Type :w followed by a file name to save it after you’ve entered the command mode by pushing Esc or Ctrl+[ and then you can issue :q to leave it. You can also use :w followed by a file name if you’ve opened a file for editing and want to save it under a different file name before you exit.
Remember that you need to type the colon for any command that has it, because it’s actually part of the command, but that you shouldn’t type it otherwise. For instance, you can type ZZ with no colon to save your changes if you’ve made any but exit the editor without saving if there are no changes. It’s slightly faster than writing changes if there were none. Another way to put this is hold down shift and then push and release the Z key twice. It’s easy to think of it like a keyboard shortcut.
While this might all seem very complex, it gets much easier with practice. You can type vim and push enter to get into it from any regular command line, and then practice these exit techniques to get better at them. At times you might find that if you just start typing, you’ll end up in the insert mode. You can always push Escape to get out of insert mode at any time. A few implementations of vi will put you in insert mode if you push the Insert key, but you can still push Esc and then type :q! to quickly exit the editor.
There is one other additional technique that you can use as well. Depending on your version of vi, you can type 😡 and push enter to exit. Please keep in mind that this won’t work with all versions of vi, but in those that it does it will write out a modified file pretty much the same way :wq will.