How To Exit Vim?

Find out how you can quit the Vim editor

  • By Daniyal Hamid
  • January 20, 2020
  • Comments
In This Article

In order to quit vim:

  1. Exit the edit mode by pressing the ESC key;
  2. Type-in :q, or one of the other exit commands listed in this article, to quit;
  3. Press the ENTER key.

Safe-quit

The following commands will fail if there are unsaved changes:

Command Description
:q[uit]
  • Quits the current window;
  • Quits Vim if it's the last / only window;
  • Fails if there are any unsaved changes.
:qa[ll]
or :quita[ll]
  • Quits all windows;
  • Quits Vim;
  • Fails if there are any unsaved changes in any window.

Save Changes And Quit

Command Description
:wq
  • Saves the current file, even if it was not changed (updating the file modification time);
  • Quits Vim;
  • Fails if the file is read-only, or the file has no name.
:wqa[ll]
  • Same as :wq;
  • Applies to all open windows.
:wq!
  • Same as :wq;
  • Force-writes read-only files.
:wqa[ll]!
  • Same as :wq!;
  • Applies to all open windows.
:x[it]
or :exit
or ZZ
  • Saves the current file (only if there were changes);
  • Quits Vim;
  • Fails if the file is read-only, or the file has no name.
:xa[ll]
  • Same as :x;
  • Applies to all open windows.

Discard Changes And Quit

Command Description
:q[uit]!
or ZQ
  • Quits Vim without checking for changes;
  • All changes in the current window are discarded.
:qa[ll]!
or ::quita[ll][!]
  • Same as :q!;
  • Changes in all open windows are discarded.

Prompt-quit

The following commands will prompt if there are unsaved changes:

Command Description
:conf[irm] q[uit]
  • Quits all windows;
  • Prompts if there are any unsaved changes in any window.
:conf[irm] xa[ll]
  • Saves (writes) all changed files;
  • Quits Vim;
  • Brings up a prompt when some files cannot be written to.

Use Vim's Help

If you feel lost, or do not understand something, then you could refer to Vim's extensive help by doing the following:

  1. Press the ESC key;
  2. Type-in :help;
  3. Press the ENTER key.

There are of course other commands to exit Vim, but the ones in the article are meant to cover most of the common cases. If you would like to share something with us, please let us know in the comments section.