Heading image for post: How To Quit Vim

Vim

How To Quit Vim

Profile picture of Josh Branchaud

A lot of people would have you believe that Vim is near impossible to quit. I'm here to dispel the rumors and show that there are quite a few ways to quit Vim.

The Basic Quit

First thing if you start up a new Vim session with just vim, you'll see a sort of splash screen. Below the title and copyright information is this line:

type  :q<Enter>               to exit

This is a good place to start because it is perhaps the most straightforward way to quit a Vim session -- the :q or :quit command. It does come with some minor caveats.

Here is the first caveat. If the current file (or some other file open in another buffer) has been edited, but not yet saved, then Vim will stop you from quitting. It wants to make sure you don't accidentally lose your changes.

If you're sure you want to quit and discard any unsaved changes, then you can use a more forceful version of the quit command -- :q!. This will quit without writing changes to the modified file.

The other caveat to the :quit command is that what it is really doing is quitting the current window. If you have split windows or multiple tabs open, then :q will only quit the one that is in focus.

Save And Quit

More often than not -- whether we use Vim as our primary editor or just need it occasionally to edit files on a remote server -- we want to save the changes we make. We can write the changes to the current file to disc and quit in one command with :wq.

Something to note about the :wq command is that it will always do a disc write, even if the file hasn't actually been edited. That means the modified date on the file will be updated even if no actual changes were made to the file. If you'd like only do a disc write when the file has actually changed, then you should use the :x command. It is short for :xit. It will write only if changes have been made and then quit.

Normal Mode Quits

There are two keybindings that allow you to quit from Normal mode, rather than Command mode.

The ZZ binding can be used to write and quit. It works in the exact same way as :x. You may find it easier to hold shift and hit Z twice than to fumble for the : key. Do what works best for you.

There is an accompanying ZQ binding which quits without checking for changes. It works in the same way as :q!.

Rage Quit

Since the :q command only quits the current window or tab, we have the :qa (or :qall) command which we can use to quit all of it at once. Vim will only quit files that are unmodified. If you'd like to quit everything and ignore unwritten changes, use the more forceful version, :qa!

Another command that behaves similarly is :cq. Vim describes this command like so:

Quit always, without writing, and return an error code.

It works nearly the same as :qa, but returns a non-zero exit code. One place in particular that I have found this useful is quitting out of a Git commit message screen, especially during an amend, when I want to back out of the commit.

Conclusion

Vim gets a tough rap for being hard to use and hard to quit. This post shows that there are quite a few different ways to quit a Vim session. Each of these ways can be utilized in different situations depending on the effect you want quitting to have.


Cover photo: unsplash-logoAlexander Andrews

More posts about Vim