How to Exit Vim Easily

How do you exit Vim? On the surface, it may seem like a strange question. However, part of what makes Vim a powerful editor, is its flexibility. Often times, there is more than one way to do something. This article will describe various ways to exit Vim. The easiest way to exit Vim is to hit ESC then type


This will save the file, if any changes have been made, then exit Vim.

Many ways to exit Vim

You can quit, or exit, Vim using ESC :q, which quits the current window. If you mistakenly edit a read-only file, use ESC :q! to quit without saving, which is a like a force quit.

Sometimes you want to write (save) the file, before you quit. To do both, you can use ESC:wq. This explicitly saves the file, then quits. This is slightly different from the :x above, where it only writes the file if there are changes.

There’s another command ZZ, which doesn’t use the colon before it. The ZZ does the same thing as :x. If they do exactly the same thing, why do you need both? In a word – choice. There may be a historical reason why ZZ exists, but I don’t know it. Try it out, do you like using ZZ instead of :x? Go for it. If not, don’t use it. It’s up to you. I used :wq for many years, before I learned about :x from someone else.

All the options you just read about, operate on the current file. What if you want or need to save your changes to a different file? You can preserve them by writing them to a new file like so:

:x newfile.txt

This will write the changes to the newfile.txt file, leave the current file unchanged, and close Vim. If you want to write a new file, but not close vim, you can use :w instead, like so:

:w newfile.txt

You should be aware though, that after newfile.txt is saved, the current file wont change. You’ll still be editing file.txt, or whatever the original filename is. This is different that what you might expect in another editor.

Exit Vim From Multiple Files

Each of the options discussed so far, are used when editing a single file. There are situations where you made have used vim to edit multiple files. Should you need to do that, can add the all options when. here are a list of examples.

:xa    exit (saving if necessary) all files 
:qa    quit all files
:qa!   force quit all files  
:wqa   save and quit all files


Vim is flexible, because its creators know that there isn’t always one right way, because your needs change according to the situation you’re in. It can help to know when you’d want to use the various ways. That’s why I wrote this post.

So what’s the right answer? Most of the time, you’ll probably want :x on your current file. This is the best way to close Vim, because you already know that in graphical editors, CTRL+X is used to exit. This uses your existing association of the letter ‘x’, with exiting an application. If you edited a file, you don’t have permissions for, use :q! to force quit Vim.

I hope that helps. Now that you know, it’s time to stop with the OMG! how do you exit Vim? jokes.

Happy computing!

Sorry, but comments are closed. I hope you enjoyed the article