MeshWorld India Logo MeshWorld.
HowTo Vim Vi Terminal Linux Text Editor Developer Tools 6 min read

How to Exit vi and Vim: Save, Quit, and Force Close

Vishnu
By Vishnu
How to Exit vi and Vim: Save, Quit, and Force Close

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

CommandWhat it doesWhen to use
:wSave without quittingYou want to keep working
:qQuit (only if no unsaved changes)Clean exit, no changes made
:wqSave and quitNormal exit with changes
:xSave and quit (same as :wq)Normal exit with changes
:q!Force quit, discard changesMade mistakes, don’t want to save
ZZSave and quit (shortcut)Fast exit without :
ZQQuit 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:

  • :q closes current file (if no changes)
  • :qa quits 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:

  • :q closes current pane
  • :qa closes all panes
  • Ctrl+w w switches 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:

  1. Esc — Return to Normal mode
  2. :wq — Save and quit
  3. :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.