Post

How to Exit Vim

How to Exit Vim

<%= page.data.title %>

The Problem

You’ve just opened Vim, maybe by accident or because someone told you it’s a great editor. Now you’re stuck. You’ve tried everything — clicking, pressing random keys, nothing works. How do you get out?

This is one of the most common questions in the programming community, and for good reason. Vim’s modal editing model is powerful, but it’s not intuitive when you’re first starting out.


The Solution

Assuming you’re already inside the Vim editor without any pending modifications to save:

  1. Press the ESC key to make sure you’re in normal mode (you don’t need to understand what normal mode is for this action)
  2. Press colon (:) followed by the letter q to get:
1
:q
  1. Press the ENTER key, and you’ll be out of Vim!
1
2
# The complete sequence
ESC + : + q + ENTER

What If You Have Unsaved Changes?

If you’ve made changes and try to quit with :q, Vim will show an error:

1
E37: No write since last change (add ! to override)

In this case, you have two options:

Option 1: Save and Quit

1
2
3
:wq
# or
:x

Option 2: Quit Without Saving

1
:q!

The ! forces Vim to quit even if you have unsaved changes.


Common Commands Reference

CommandWhat It Does
:qQuit (no unsaved changes)
:q!Force quit (discard unsaved changes)
:wWrite (save) file
:wqWrite and quit
:xWrite and quit (same as :wq)
ZZWrite and quit (shortcut)
ZQQuit without saving (shortcut)

What I Learned

  1. Vim’s modal model makes sense once you understand it — The reason Vim is “hard” to quit is that it expects you to be in a specific mode. ESC always takes you to normal mode, which is the safe starting point for any command.

  2. The basics are simple — You can be productive in Vim with just a handful of commands. :q, :w, :wq, and movement keys cover 80% of use cases.

  3. Don’t be afraid — The worst thing that can happen is you lose your unsaved changes. Learn to save frequently, and you’ll be fine.

  4. Keep learning — Once you can exit, you can start exploring. Vim’s efficiency comes from staying in the editor and using keyboard commands instead of reaching for the mouse.


Resources


Be careful next time!

This post is licensed under CC BY 4.0 by the author.