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 as a different file |
:up | Save only if there are unsaved changes |
:wq | Save and quit |
: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.
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.
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.
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
If you open a file without the proper permissions, Vim will show E212: Can't open file for writing when you try to save.
If you have sudo privileges, you can save the file without reopening it:
w !sudo tee % > /dev/nullThis writes the buffer to disk using sudo. Vim will ask you to confirm. Press L to reload the file or O to keep the current buffer.
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.
What does E45: ‘readonly’ option is set mean?
You opened the file with vim -R or view, which sets read-only mode. To save anyway, use :w! to override the read-only flag. If the issue is file permissions, use 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 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page