You ran a git commit, a server setup script, or just opened the wrong file — and now you’re in Vim. The cursor blinks. Nothing you type seems to do anything useful. You try Ctrl+C. Nothing. You try Ctrl+Z. You’re now in a suspended process. You close the terminal window and pretend it didn’t happen.
This guide is the one you need to never do that again.
How to exit Vim / Vi
Vim has modes. When you open it, you’re in Normal mode — where keys are commands, not characters. Before running any exit command, make sure you’re in Normal mode by pressing Esc (press it once or twice to be sure).
Exit without saving (discard all changes)
:q!
The ! forces the quit even if there are unsaved changes. This is the “get me out of here” command.
Save and exit
:wq
w writes (saves) the file. q quits. You can also use :x — same effect.
Save without exiting
:w
Writes the file and keeps you in the editor.
Exit only if there are no unsaved changes
:q
Vim will refuse to quit if you have unsaved changes. Use :q! if you want to discard them, or :wq to save first.
Quick reference — Vim exit commands
| Command | What it does |
|---|---|
:q | Quit (only works if no unsaved changes) |
:q! | Force quit, discard all changes |
:wq | Save and quit |
:x | Save and quit (same as :wq) |
:w | Save without quitting |
ZZ | Save and quit (shortcut, no colon needed) |
ZQ | Quit without saving (shortcut, no colon needed) |
What if Vim is frozen or unresponsive?
If the terminal is hanging, try Ctrl+C to cancel any running command first. If you’re back in Normal mode, use :q!. If the terminal itself is stuck, you can close it — your file changes won’t be saved, but Vim creates a swap file (.filename.swp) in the same directory. Next time you open the file, Vim will offer to recover from it.
How to exit Nano
Nano is much friendlier than Vim. The commands are shown at the bottom of the screen. But if the screen is too small to display them, here’s what you need:
Exit Nano
Ctrl + X
If you have unsaved changes, Nano will ask:
Save modified buffer?
Y - Yes
N - No
^C - Cancel
Press Y to save, N to discard changes, or Ctrl+C to cancel and go back to editing.
Save without exiting
Ctrl + O
Nano will ask you to confirm the filename. Press Enter to save.
Nano quick reference
| Shortcut | What it does |
|---|---|
Ctrl + X | Exit (prompts to save if needed) |
Ctrl + O | Save file (write out) |
Ctrl + C | Cancel current action |
Ctrl + Z | Suspend Nano (returns to terminal) |
How to exit Vi (classic)
Vi is Vim’s older ancestor, still present on many minimal Linux/Unix systems. The exit commands are the same as Vim:
:q! ← quit without saving
:wq ← save and quit
If you’re on a very old system where :wq doesn’t work (rare), try :x or ZZ.
Why Vim feels so hard to exit
Vim’s Normal mode is the culprit. Every key is a command, not a character. When you open Vim, you land in Normal mode — so pressing letters like i, a, w doesn’t type them, it runs commands (i enters Insert mode, w jumps to next word, etc.).
The pattern to remember:
- Press
Escto make sure you’re in Normal mode - Type
:— a colon appears at the bottom of the screen - Type your command (
q!,wq, etc.) and pressEnter
That’s it. Once this clicks, Vim stops being terrifying.
The one command to remember
If you remember nothing else from this guide:
Esc → :q! → Enter
Gets you out of Vim every time, no matter what state you’re in.
Want the full Vim command reference? Check the Vim Cheat Sheet.
Related Reading.
Vim Cheat Sheet: Modes, Navigation, Editing & Config
Complete Vim reference covering modes, movement, editing commands, search and replace, visual mode, split panes, macros, text objects, and a starter .vimrc.
Install Spotify on Ubuntu
Learn how to install Spotify on Ubuntu using Snap or Spotify's official Debian repository, plus how to update and remove it later.
Install Telegram on Ubuntu
Learn how to install Telegram Desktop on Ubuntu using Snap or the official Linux tarball, then update or remove it cleanly.