10 Ways to Exit Vim Editor

Vim Exit Featured Image

Let’s talk about the different ways to exit vim editor here. As you would know Vim is a Linux utility for editing text. The special aspect of only using the keyboard to edit makes it a unique editor. With this aspect comes a difficulty that you can not exit the editor by clicking on the ‘cross’ button. There is no pop-up window, which confirms your decision of quitting. That is the main reason, thousands of computer experts sometimes stumble upon the problem of “how do I exit Vim?”.

Exit Vim Meme

Let’s see the 10 different ways to exit vim editor

1. Simply Exit Vim

After editing the text and saving your progress, you simply type :q or :quit in the editor. As soon as you press ':', outside the insert mode, you will notice the commands appearing on the bottom of the screen.

Vim Simple Quit
Simple Quit

Note: In case you can not see the command :q at the bottom of the screen, that means you are still inside the insert mode. To exit the insert mode, press ESC (escape) key.

However, it may happen that you forget to save what you edited or you edited but do not wish to save. Vim is unable to make such differences and confirms your decision of quitting. This task of reassurance is shown by an error on the bottom of the screen.

Vim Simple Exit Error
Error while quitting

The error E37 means that you have not saved (write) after the last change. In order to quit no matter what, you have to override the quitting command by using the exclamation mark.

2. Force Quit Vim

If you desire to discard any changes done to the text and just exit the Vim editor, then Vim has a forced quit command :q!

Vim Forced Quit
Forced Quit

3. Confirm and Quit

Similar to the error mentioned above, E37, Vim provides a confirmation in case you wish to quit after making some changes to the text. To make use of this confirmation, type :conf q and press ENTER.

Vim Confirm Quit
Confirm while quitting

You will press the key S if you wish to save the changes, otherwise press N. To prevent the editor from closing, you can enter C.

Note: This confirmation message only appears if there have been some changes made to the existing file, otherwise it will close automatically.

4. Save and Exit Vim editor

After making some progress in the buffer, you now wish to save all the changes and continue on your next venture. In order to save and quit in one single command, Vim has :wq.

Vim Save Exit
Save and Quit

This command initially does a disc write (:w), and follows with the simple quit (:q).

5. Save (if necessary) and Quit

To save the system from unnecessary disc write, Vim has a feature of saving ‘only if’ the text was altered. To make use of this feature, the command is :x.

Vim Save If Changed Quit
Save (if necessary) and Quit

6. Save (if necessary) and Quit (Alternative)

As an alternative to :x for performing necessary save and quit, Vim has a cool way to quit that does not even require the ENTER key. Pressing SHIFT + ZZ will do the same job. After pressing it the first time, you can see Z on the bottom of the screen and as soon as you press it the second time, Vim is already closed.

Vim Save Quit Z
After the first SHIFT + Z

7. Force Quit (Alternative)

Instead of using :q! to force quit from Vim editor, you can take advantage of another force quit method, by pressing SHIFT + ZQ. Similar to the previous technique, after feeding in SHIFT + Z, you will see Z on the bottom right of the editor. Just as you press SHIFT + Q, the editor will close automatically, no matter your progress.

8. Quit All

When your day’s job is finished and you want to quit all the files at once, you can do so by the command :qa or :qall. It exits from all the saved files at once and may stop at those which have unsaved changes. In order to, force quit all of them, :qa! or :qall! comes in handy.

Vim Quit All
Quit all

9. Quit All with Error Code

Similar to :qa!, the command :cq or :cquit exits from all files, but with a slight difference. When it exits, it sends out an error code. The system understands the type of exit from the error code and stops the file from being compiled. Since there has been no saving, all the changes are lost.

Vim Cancel Quit
Quit with an error code

10. Save and Quit All

After finishing your project if you wish to close all your programs and save them all, then you can enter the command :wqa or :wqall. This command will write all the buffers and exit.

Vim Write Quit All
Write and Quit all files


Vim documentation : editing – For more variations on writing and quitting files using Vim.