Have you ever wanted to work more with Vim but are held back because you’ve gotten stuck exiting it? Some editors, like Nano, are easier to manage because they give you a cheat-list of commands right on the terminal. However, Vim is not too difficult to get used to if you give it some time.
In this guide, we will go over how to exit Vim in the terminal. We will start off by opening and writing to a file in Vim. We will then introduce and test the various keys and commands you can use to exit Vim.
To write to a file you will need to enter insert mode. To open or save a file and to exit Vim, you input commands or key combinations in command mode.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
Do you want to learn more about how a coding bootcamp can help you learn to code? Get started by finding the right bootcamp for you along with unlocking additional information about bootcamp cost and reviews.
Perhaps you are in Vim (you’ve entered vim followed by your file name) but can’t seem to get anything done. Even if you opened the file, you have to press the i key first (“i” for insert).
The problem comes when we are done writing to our file and can’t seem to figure out how to correctly exit! Let’s take a look at our exit commands. We will start with esc to escape insert mode.
How to Exit Vim
To exit Vim, you must first switch to command mode. This is the mode where your text is interpreted as commands rather than as text to write to a file.
When you go into command mode, you can save the file (or not, up to you!), quit, or a combination thereof. If all you want to do right now is to save your changes before you exit, you can type the :w command followed by the Enter key. Vim will then output the file name, whether it’s new, lines written to it, and a character count:
Now let’s go over some other commands you may want to use to exit Vim. These commands are typed in the command mode, which means you must have exited insert mode with the esc key if needed (like we did in the previous example).
Note that the commands we mention are prefixed by a semi-colon “:”. This semi-colon signifies we are typing a command. When you are done typing the command, press Enter.
To exit Vim, use the :q (“q” for quit) command. In the last example, use :q to exit that screen and go back to your normal terminal prompt. Your contents will not be saved. If you wrote something new to the file but haven’t saved that new content yet, you will get an error message. Depending on what you reply to the message, you can override the unsaved status or you can be sent back to the screen you were in before using :q.
To exit Vim and not save changes, use the :q! Command. Try inserting some text to your test file, pressing esc to exit insert mode, and using the :q! command to exit without saving changes. Then open it again and you will see that whatever you typed was not saved (after you’re done checking it remember to use :q to go back to your terminal!).
To exit Vim and save changes, use the :wq command. This command is a combination of the write (:w) and quit (:q) commands we have tried separately in the above examples.
To exit Vim and save changes, use the 😡 command. The difference between this and the :wq command is that, in the background, 😡 will log modification time only if changes have been made, whereas :wq will do it regardless. This makes no difference at all to your files. It only matters if modification time is being monitored to keep track of changed files.
If you want to skip having to enter command mode using the esc key and then writing your commands, you can use the following key combinations:
- To save a file and exit, press on esc and shift while double-clicking Z
- If you choose not to save your changes, press on esc and shift while clicking Z and X
As a reminder, here are the ways to exit Vim:
- :wq or 😡 → write/save and quit
- :q → quit (does not work if there are unsaved changes)
- :q! → quit and discard unsaved changes
- :wqa →write/save and quit on all tabs
Now you are equipped with several methods to exit the Vim text editor. We have reviewed ways to quit the Vim text editor and save your changes (or not save, that’s up to you).
Love the power and flexibility of Vim but are bored of seeing the same, old terminal interface? There are lots of fun emulators you can use to spice up your display. Also, most people use Vim in a terminal, but there is also a GUI (Graphical User Interface) you can try out called gVim.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.