You’re stuck in vi or Vim. The screen is frozen. Nothing you type appears. You just want to get out.
This happens to every developer at least once. vi and Vim are powerful editors, but their modal design makes exiting confusing for beginners. This guide covers every way to exit vi/vim, when to use each method, and what to do when you’re truly stuck.
Quick Reference: All Exit Commands
| Command | What it does | When to use |
|---|---|---|
:w | Save without quitting | You want to keep working |
:q | Quit (only if no unsaved changes) | Clean exit, no changes made |
:wq | Save and quit | Normal exit with changes |
:x | Save and quit (same as :wq) | Normal exit with changes |
:q! | Force quit, discard changes | Made mistakes, don’t want to save |
ZZ | Save and quit (shortcut) | Fast exit without : |
ZQ | Quit without saving (shortcut) | Fast force quit without : |
The One Thing to Understand First
vi and Vim have modes. This is why you can’t just close them like other editors.
- Normal mode (default): Keys are commands, not text
- Insert mode: Keys type characters
- Command mode: Type commands after
:
To exit, you must be in Normal mode first. Press Esc to return to Normal mode from anywhere. If you’re already in Normal mode, pressing Esc won’t hurt — it just confirms you’re there.
If you’re stuck and nothing works: press Esc a few times, then try the exit commands.
How to Exit: Step-by-Step Methods
Save and Quit (Normal Exit)
Use this when you’ve made changes you want to keep.
Method 1: Command mode
Esc
:wq
Enter
Method 2: Shortcut
Esc
ZZ
Both save your changes and close the editor. :wq and :x are identical — use whichever you remember.
Quit Without Saving (Discard Changes)
Use this when you made mistakes or opened a file just to read it.
Method 1: Command mode
Esc
:q!
Enter
Method 2: Shortcut
Esc
ZQ
The ! forces vi to quit even if you have unsaved changes. Warning: this cannot be undone.
Save Without Quitting
Use this when you want to keep working but save your progress.
Esc
:w
Enter
You can also save to a different filename:
:w newfile.txt
Quit Only If No Changes
Use this when you haven’t made any changes or just opened a file to read.
Esc
:q
Enter
If you have unsaved changes, vi will refuse and show an error. Use :q! to force quit instead.
When to Use Each Method
Scenario 1: You just edited a config file and want to save
Use: :wq or ZZ
You made changes intentionally. Save them and exit.
Scenario 2: You opened a file to read it, didn’t change anything
Use: :q
No changes to save. Clean exit.
Scenario 3: You accidentally deleted important content
Use: :q! to discard, then reopen the file
Don’t save the mistake. Force quit and start fresh.
Scenario 4: You’re experimenting and don’t care about changes
Use: :q! or ZQ
Quick exit without saving anything.
Scenario 5: You want to save your progress but keep working
Use: :w
Save now, continue editing later.
Common Stuck Scenarios
”I can’t type anything!”
Problem: You’re in Normal mode but think you should be typing.
Solution: Press i to enter Insert mode, or press Esc then :q! to quit.
”It says ‘E37: No write since last change’”
Problem: You have unsaved changes and tried :q.
Solution: Use :wq to save and quit, or :q! to discard changes.
”I opened a file with sudo but can’t save”
Problem: You opened a read-only file (like /etc/hosts) without proper permissions.
Solution: Quit with :q!, then reopen with sudo vim filename.
”I have multiple files open and can’t close one”
Problem: You opened multiple files with vim file1 file2.
Solution:
:qcloses current file (if no changes):qaquits all files:qa!force quits all files
”I’m in a split pane and can’t exit”
Problem: You used :sp or :vsp to split the window.
Solution:
:qcloses current pane:qacloses all panesCtrl+w wswitches between panes
”I pressed Ctrl+S and everything froze”
Problem: You accidentally enabled flow control (XOFF). This freezes the terminal.
Solution: Press Ctrl+Q to unfreeze. This is a terminal feature, not vi.
Troubleshooting When Commands Don’t Work
Press Esc multiple times
If nothing responds, you might be in a special mode. Press Esc 3-4 times to return to Normal mode.
Check for modified files
If :q refuses, check what’s modified:
:files
This shows all open files and their status (+ means modified).
Force quit as last resort
If nothing else works and you just want out:
:q!
Or kill the process from another terminal:
pkill vim
# or
killall vim
FAQ
Is vi the same as Vim?
Vim is “Vi IMproved” — a modern fork of vi with more features. The exit commands are identical for both.
Why is vi so hard to exit?
vi was designed in 1976 for terminals without arrow keys. Its modal design (separate modes for navigation vs typing) was efficient then but confusing now. Once you understand modes, it makes sense.
Can I change the default editor to something easier?
Yes. Set your default editor in your shell config:
# For bash, add to ~/.bashrc
export EDITOR=nano
# For zsh, add to ~/.zshrc
export EDITOR=nano
Or for Git specifically:
git config --global core.editor nano
What if I’m in Nano instead of vi?
Nano is easier: Ctrl+X to exit, then Y to save or N to discard.
See our How to Exit Nano guide for complete Nano instructions.
How do I practice without risking real files?
Create a test file:
vim test.txt
Experiment with all the commands. When done, use :q! to discard.
Summary: The Commands You Actually Need
Memorize these three and you’ll handle 90% of situations:
Esc— Return to Normal mode:wq— Save and quit:q!— Force quit without saving
Everything else is a nice-to-have.
Want to master Vim beyond just exiting? Check out our Vim Cheat Sheet for navigation, editing, and advanced commands.
Related Articles
Deepen your understanding with these curated continuations.
How to Exit Nano: Save, Quit, and Force Close
Complete guide to exiting Nano editor — save changes, quit without saving, force quit, and handle common stuck scenarios with clear examples.
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.
How to Exit Vim, Vi, and Nano (Step by Step)
Stuck in Vim? Here is exactly how to save and exit, quit without saving, and force-quit Vim, Vi, and Nano — with every command you need and why they work.