M
MeshWorld.
HowTo Vim Nano Linux Terminal Bash Beginner Text Editor 4 min read

How to Exit Vim, Vi, and Nano (Step by Step)

By Vishnu Damwala

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

CommandWhat it does
:qQuit (only works if no unsaved changes)
:q!Force quit, discard all changes
:wqSave and quit
:xSave and quit (same as :wq)
:wSave without quitting
ZZSave and quit (shortcut, no colon needed)
ZQQuit 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

ShortcutWhat it does
Ctrl + XExit (prompts to save if needed)
Ctrl + OSave file (write out)
Ctrl + CCancel current action
Ctrl + ZSuspend 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:

  1. Press Esc to make sure you’re in Normal mode
  2. Type : — a colon appears at the bottom of the screen
  3. Type your command (q!, wq, etc.) and press Enter

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.