1. Save and quit

Esc
:wq
Enter

The :w writes (saves) the file and :q quits. Together :wq does both. You can also use the shortcut :x, which saves only if there are changes.

2. Quit without saving

Esc
:q!
Enter

The exclamation mark forces Vim to quit and abandon any unsaved changes. Use this when you opened a file by accident.

3. Quit a file you did not change

Esc
:q
Enter

Plain :q works only when there is nothing to save; otherwise Vim warns you and refuses to quit.

Common mistakes

  • Forgetting to press Esc. If you are in insert mode, the colon just types a character. Press Esc first.
  • Typing the commands in the wrong case. :WQ will not work — Vim commands are case-sensitive.
  • Seeing an E37 error. That means unsaved changes; use :wq to save or :q! to discard.

Frequently asked questions

What is the difference between :wq and :x?

Both save and quit, but :x only writes the file if it actually changed, while :wq always writes. In practice they behave almost the same.

How do I exit Vim without saving?

Press Esc, then type :q! and press Enter. The ! forces the quit and throws away unsaved changes.

New to the terminal? See our complete guide to learning to code.