How to Exit Vim

"How do I exit Vim?" is one of the most-searched programming questions, and the answer is simple once you know that quitting happens in Normal mode. Here is how to leave Vim cleanly, whether or not you have unsaved changes.

Steps

  1. Return to Normal mode. Press Esc. Vim only accepts quit commands from Normal mode, so this is always the first step.
  2. Quit when there are no unsaved changes. Type :q and press Enter.
  3. Quit and discard unsaved changes. Type :q! and press Enter to abandon your edits and exit.
  4. Save and quit. Type :wq (or press Z Z) and Enter to write the file and exit in one step.

In the Vim Browser Editor

In the Vim Browser Editor there is no separate program to close, so :q reports "Nothing to quit (running in the browser)." Use :w to save your work, then simply close or navigate away from the browser tab.

Try it in the editor

Related how-tos

Advertisement