How to Exit Vim?

There are many commands to exit Vim, so in this article we plan to cover most of the common cases.

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.

This post was published (and was last revised ) by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.