How to Save a File in Vi / Vim & Exit
How to Save a File in Vi / Vim & Exit

How to Save a File in Vi / Vim and Exit (:wq, :x, ZZ)

Quick Answer: To save a file in Vi/Vim and exit, press Esc to enter normal mode, then type :wq and press Enter. You can also use :x or ZZ to save and quit. To quit without saving, type :q! and press Enter.

The Fast Answer (Save and Exit Vim)

  1. Press Esc to make sure you’re in normal mode.
  2. Type :wq (write & quit).
  3. Press Enter.

That’s it. If :wq doesn’t seem to work, you’re almost always still in insert mode — press Esc first, then try again.

Why You Can’t Just Type to Save (Vim Modes)

Vim has different modes, and this trips up almost every beginner:

  • Normal mode — for commands (saving, quitting, navigating). Vim starts here. Press Esc to return to it any time.
  • Insert mode — for typing text. You enter it with i, a, or o.
  • Command-line mode — the line at the bottom that starts with :, where you type commands like :wq.

Save and quit commands only work from normal mode, which is why you press Esc first.

All Save & Quit Commands in Vi/Vim

CommandWhat it does
:wSave (write) the file, stay in Vim
:wqSave and quit
:xSave and quit (only writes if there are changes)
ZZSave and quit (no colon needed — just press Shift+Z twice)
:qQuit (only works if there are no unsaved changes)
:q!Quit and discard all unsaved changes
:wq!Force save and quit (e.g. read-only file you own)
ZQQuit without saving (no colon needed)
:w filenameSave as a new file name
:wa / :wqaSave all / save all and quit (multiple files)

Save Without Exiting

To save your work but keep editing, press Esc, then type :w and press Enter. The file is written to disk and you stay in Vim.

Save and Exit

Any of these save the file and close Vim (press Esc first):

:wq      # write and quit
:x       # write (if changed) and quit
ZZ       # write and quit — shortcut, no Enter needed

Quit Without Saving (Discard Changes)

If you made changes you don’t want to keep, press Esc and type:

:q!      # quit and throw away unsaved changes
ZQ       # same thing, shortcut

Save a Read-Only File (Permission Denied)

If you opened a system file without sudo and get “E45: ‘readonly’ option is set”, you can still save it with this trick:

:w !sudo tee %
# then type your password, and :q! to exit

Or force-save a file you own that’s marked read-only with :wq!.

Help! I’m Stuck in Vim

The classic situation. To get out:

  1. Press Esc a couple of times (this exits insert mode and cancels any half-typed command).
  2. To leave and keep changes: type :wq and Enter.
  3. To leave and discard changes: type :q! and Enter.

Vi vs Vim — Does It Matter?

No — all the commands above work the same in both vi and vim (Vim is “Vi Improved”). On most modern Linux systems, typing vi actually launches Vim. Master these commands and you can edit files on any Linux server.

Want more command-line skills? See our complete guide to Linux file editors (nano, vi, vim), Essential Linux Commands, and Linux for DevOps Engineers.

Frequently Asked Questions

How do I save and exit Vim?

Press Esc, type :wq, and press Enter. Alternatives are :x or ZZ.

How do I quit Vim without saving?

Press Esc, type :q!, and press Enter to discard all changes and exit.

What is the difference between :wq and :x?

Both save and quit. :wq always writes the file (updating its timestamp); :x only writes if there were actual changes.

Why won’t :wq work?

You’re almost certainly still in insert mode, so the characters are being typed into the file. Press Esc first, then type :wq.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *