How to Save a File and Exit Vim / Vi

Vim is a terminal-based text editor that comes preinstalled on macOS and almost all Linux distributions. Unlike other editors, Vim has several modes of operation, which can make saving and quitting unintuitive for new users.
To save a file and exit Vim, press Esc, type :wq, and press Enter. To quit without saving, use :q! instead. This guide explains how to save a file in Vim, quit the editor, and handle common situations like read-only files.
Quick Reference
For a printable quick reference, see the Vim cheatsheet .
| Command | Description |
|---|---|
:w | Save the file |
:w filename | Save a copy under another name |
:saveas filename | Save under a new name and keep editing it |
:up | Save only if there are unsaved changes |
:wq | Save and quit |
:wq! | Save and quit, overriding the read-only flag |
:x | Save (if changed) and quit |
ZZ | Save (if changed) and quit (shortcut) |
:q | Quit (only if no unsaved changes) |
:q! | Quit and discard changes |
ZQ | Quit and discard changes (shortcut) |
:wa | Save all open buffers |
:qa | Quit all buffers |
:qa! | Quit all buffers, discard changes |
:wqa | Save all and quit |
:w !sudo tee % | Save a read-only file with sudo |
Vim and Vi
On many Linux systems and on macOS, typing vi starts a Vim build. Debian and Ubuntu normally route vi through the alternatives system to a Vim build such as vim.tiny, Fedora and RHEL ship /usr/bin/vi from the vim-minimal package, and on macOS /usr/bin/vi is a symbolic link to vim. To confirm what you are actually running, ask it for its version:
vi --version | head -1VIM - Vi IMproved 9.1 (2024 Jan 02, compiled Apr 18 2026 20:29:32)If the first line mentions Vim, everything in this guide applies exactly as written.
POSIX specifies the :w, :wq, :q, :q!, and :x commands through ex, while ZZ is specified separately as a vi normal-mode command. These commands are the most portable choices for original vi and cut-down builds such as BusyBox vi. The :up command and the multi-buffer commands :wa, :qa, and :wqa are Vim additions. ZQ is also not required by POSIX, although some vi clones, including current BusyBox vi, support it.
Vim Modes
Vim operates in different modes. The two you need to know for saving and quitting are:
- Normal mode - The default mode when you open Vim. You run commands in this mode.
- Insert mode - Activated by pressing
i. This mode lets you type and edit text like a regular editor.
To switch from insert mode back to normal mode, press Esc. All save and quit commands in this guide are run from normal mode. Typing : opens the command line at the bottom of the screen. You type the command there and press Enter to run it.
Opening a File
To open a file in Vim, type vim followed by the file name:
vim file.txtIf the file does not exist, Vim creates a new buffer for it. The file is written to disk only when you save.
You can also open a file from inside Vim by typing :e file.txt and pressing Enter.
Saving a File
The command to save a file in Vim is :w.
- Press
Escto switch to normal mode. - Type
:wand press Enter.

The file is written to disk and you remain in the editor, so you can carry on working and save again as often as you like. This is the command to reach for in the middle of a long edit, such as a find and replace pass across a config file.
To save the file under a different name, type :w new_filename and press Enter. This writes a copy, but you are still editing the original file, and the next :w writes to the original name. To switch the buffer to the new name and keep working on it, use :saveas new_filename instead.
If a file with that name already exists, Vim refuses to overwrite it and shows E13: File exists (add ! to override). Use :w! new_filename to replace it.
If you started Vim without a file name, :w has nowhere to write and fails with E32: No file name. The same happens with :wq. The :x command reports this error only if the buffer has unsaved changes; otherwise, it exits without writing. Pass a name to the command, for example :w notes.txt or :wq notes.txt, and Vim uses it as the file name for later saves.
The :up (update) command is similar to :w but only writes to disk if the buffer has unsaved changes. If nothing has changed, it does nothing.
Saving and Quitting
The command to save a file and exit Vim is :wq.
- Press
Escto switch to normal mode. - Type
:wqand press Enter.

The shortcut ZZ (uppercase, no colon) saves and exits without typing a colon command at all, which makes it the fastest way out of a file you have edited.
The :x command is another alternative, and it is what ZZ runs behind the scenes. Both write to disk only when the buffer has unsaved changes, while :wq writes every time and updates the file modification time. That difference matters when something is watching the file: quitting an unchanged file with :wq can trigger a make target or a file watcher to rebuild for nothing, and :x or ZZ leave the timestamp alone.
The same difference shows up on files you can only read. If you open a system file such as /etc/hosts just to look at it, :wq still tries to write and fails with E45: 'readonly' option is set (add ! to override). :x and ZZ skip the write and exit.
Quitting Without Saving
To exit Vim without saving your changes, use :q!.
- Press
Escto switch to normal mode. - Type
:q!and press Enter.

The ! forces Vim to discard all unsaved changes. Without it, Vim will show an error if the buffer has been modified.
The shortcut ZQ (uppercase, no colon) does the same thing.
Quitting is a blunt way to reverse a mistake, since it throws away every edit made since the last save. When you only want to undo the last few changes and keep the rest, press u in normal mode instead. See How to Undo and Redo in Vim
for stepping back through the undo history.
To quit Vim when you have not made any changes, type :q and press Enter. No ! is needed if the buffer is unchanged.
Saving All Files and Quitting All Buffers
When you have multiple files open in Vim, you can save or quit all of them at once:
:wa- Save all open buffers:qa- Quit all buffers (fails if any have unsaved changes):qa!- Quit all buffers and discard all unsaved changes:wqa- Save all buffers and quit
Saving a Read-Only File
When you open a file you do not have permission to write, such as a file under /etc, Vim marks the buffer read-only and shows [readonly] next to the file name. As soon as you start typing, it warns W10: Warning: Changing a readonly file. Running :w then fails with E45: 'readonly' option is set (add ! to override).
The :w! command overrides Vim’s read-only flag and can sometimes save an unwritable file if you own it or can replace it in its directory. It does not grant extra system permissions: for a root-owned file in a protected directory such as /etc, it still fails with E212: Can't open file for writing.
If you have sudo privileges, you can save the file without reopening it:
:w !sudo tee % > /dev/nullThe :w !command form sends the buffer to a shell command instead of writing the file directly. Vim replaces % with the current file name, sudo tee writes the buffer to that file as root, and > /dev/null hides the copy that tee prints back to the screen. Enter your password if sudo asks for it.
At this point the file on disk has changed, but the buffer is still marked as modified. Vim notices the mismatch and asks what to do:
W12: Warning: File "/etc/myapp.conf" has changed and the buffer was changed in Vim as well
See ":help W12" for more info.
[O]K, (L)oad File, Load File (a)nd Options:Press L to load the file from disk. The buffer now matches what tee wrote and is no longer marked as modified, so :q exits normally. If you press O or Enter instead, the buffer stays marked as modified and :q fails with E37. Your changes are already on disk at that point, so :q! is safe to use.
FAQ
What is the exact key sequence to save and quit?
Press Esc, type :wq, then press Enter. The colon opens the command line at the bottom of the screen, wq is the command itself, and Enter runs it. If you would rather not type a colon command, press Esc followed by two uppercase Z keystrokes.
What is the difference between :wq and :x?
Both save and quit, but :wq always writes the buffer to disk (updating the file modification time), while :x only writes if there are unsaved changes. If you have not made any edits, :x leaves the modification time unchanged.
How do I quit Vim if I accidentally opened it?
Press Esc to make sure you are in normal mode, then type :q and press Enter. If you accidentally typed something, use :q! to quit without saving. Pressing Ctrl+C does not exit Vim; it only prints a hint telling you which command to type. If a small window labeled [Command Line] opens, you typed q: instead of :q. Type :q once to close that window, then again to leave Vim. If recording @a (or another letter) appears at the bottom of the screen, Vim is recording a macro. Press q to stop the recording, then quit as usual.
What does E45: ‘readonly’ option is set mean?
Vim sets the read-only flag when you open a file with vim -R or view, and also when you open a file you do not have permission to write. Use :w! to override the flag, or :wq! to save and quit in one step. These commands still depend on system permissions. For a root-owned file in a protected directory such as /etc, the write fails with E212: Can't open file for writing, and you need the sudo tee method described above.
What does E37: No write since last change mean?
Vim is warning that the file has unsaved changes. Use :wq to save and quit, or use :q! if you want to discard the changes.
Can I save and continue editing?
Yes. The :w command saves the file without quitting. You can continue editing after saving.
Conclusion
Once :wq and :q! are muscle memory, the commands worth adding next are :x and ZZ, which skip the write when nothing has changed. For moving text around before you save it, see How to Copy, Cut and Paste in Vim
.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page