Vim Racer

How to Exit the Vim Editor

If you're just looking for a quick reference, jump to the quick reference manual.

The easiest way to exit vim is :x + enter. That’s :, x and the enter key. If you made any changes to the file, they will be applied before the editor closes. If you’re still stuck, you might just need to hit the esc key a few times. Hitting esc will bring you back to vim’s normal mode, and you might've been in one of vim’s other modes: insert, visual, replace etc.

:x is a replacement for :wq or :q. It doesn’t really matter which one you use, but :q won’t always work. If you have made changes to the open file, :q will result in an error because vim won’t let you abandon changes that haven’t been written to the file. The difference between :x and :wq is that :x will only write changes when the buffer is full i.e. the file was edited. Exclusively using :x will save your hard drive from unnecessary writes. However, :wq might be useful if you want to update the modification time of your file.

Alternative methods to exit vim

Ordered by most useful, here are a few other ways to exit vim.

:x to quit and write changes if the buffer is not empty.

:cq and :cq! to quit without saving and return a non-zero exit code. This is especially useful if you want to abort the git editor i.e. exiting from git commit --amend.

:qa to close all buffers and exit. You can add an exclamation point (!), to exit without a warning about losing unsaved changes.

:q! to quit without a warning about unsaved changes.

:wq to quit and write changes regardless of whether the buffer is empty or not.


Quick Reference Manual

  1. Press the esc key a few times. Alternatively, you can press [. Either of those keys will bring you back to vim’s normal mode.
  2. The next step depends on your intent:
    • If you have made changes to the file, and you want to keep them, you just need to type :, x, and enter.
    • If you have made changes to the file, and you don’t want to keep them, you just need to type :, q, !, and enter.
    • If you’re in the editor because of git, i.e. amend or merge, and you want to abort the action, you just need to type :, c, q, !. This will exit vim with a non-zero exit code, and the non-zero exit code will tell git that you want to abort.
    • If you’re in the editor because of git, i.e. amend or merge, and you want git to proceed with making changes, you just need to type :, x, and enter.