Vim is a powerful editor, but it can take a while to get used to how it works. One of the most common issues most beginners run into right away when first using Vim is knowing how to exit the Vim editor. This can be very frustrating, so we’ve made a quick guide to help you figure it out.

Beyond knowing the simple commands to exit Vim, there are also a number of other commands that are good to know when it comes to exiting Vim, saving changes, or discarding changes. We will look at a few of these.

The fastest way to exit Vim

If you’ve opened a document and you haven’t made changes to it

The fastest way to exit vim is simply to type:

(hit the esc key) :q

How to exit vim - entering command mode

or

(hit the esc key) :x

More on :x and the differences below.

Hit the Enter key to execute the command.

When you type :, it will start typing at the bottom of your window. This is how you enter commands instead of typing words in the document itself. I recommend always first pressing esc on your keyboard before typing :, because if you’ve accidentally entered the edit mode (pressing i), it will write ‘:’ in the document instead of entering command mode. So as a habit, I always press esc first.

 

If you’ve opened a document and you have made modifications to the document

If you want to save changes made:

The fastest and easiest way is:

:x

Some people instead use the following command:

:wq

The difference? Mostly personal preference, but as the documentation states:

  • :wq – Write the current file and quit. Writing fails when the file is read-only (use :wq! to write read-only files) or the buffer does not have a name.
  • :x – Like “:wq”, but write only when changes have been made. Otherwise, it simply exits.

I personally prefer :x because it can work for multiple different scenarios and, as a result, it reduces the number of commands I have to remember.

If you want to discard changes made:

:q!

If you want to save changes but it’s read-only

:wq!

This is the same as :wq, but it writes even read-only files

 

Other helpful Vim shortcuts and tips

Write changes to another file and quit

  • :wq [++opt] {file} – Write to {file} and quit.
  • :wq! [++opt] {file} – Write to {file} and quit, even if read-only.

Multiple Windows & Buffers

Most of the commands we’ve already looked at can also be applied to multiple windows and buffers if, for example, we are using Vim to edit multiple documents.

  • :xa
  • :qa
  • :wqa

and more.

Vim cheat sheet and training

We’ve only looked at exiting Vim in this article, but there is so much more you can do with Vim. For more helpful tips & tricks, you can refer to this Vim Cheat Sheet.

To learn the complete ins-and-outs of Vim, enroll in our free Vim – The Improved Editor course. All you have to do is register for a free Community Edition and you will have complete access to this course. We even have a Hands-On Lab that deploys server resources so you can practice Creating and Modifying Files with Vim in a real-world scenario. You can use Gems given to you in your Community Edition in order to launch this lab for free.

Vim - The Improved Editor course

Happy learning and happy editing with Vim!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get actionable training and tech advice

We'll email you our latest articles up to once per week.