How to Save a File and Exit Vim

By 

Updated on

5 min read

Vim Save and Quit

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 .

CommandDescription
:wSave the file
:w filenameSave as a different file
:upSave only if there are unsaved changes
:wqSave and quit
:xSave (if changed) and quit
ZZSave and quit (shortcut)
:qQuit (only if no unsaved changes)
:q!Quit and discard changes
ZQQuit and discard changes (shortcut)
:waSave all open buffers
:qaQuit all buffers
:qa!Quit all buffers, discard changes
:wqaSave all and quit
:w !sudo tee %Save a read-only file with sudo

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:

Terminal
vim file.txt

If 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.

  1. Press Esc to switch to normal mode.
  2. Type :w and press Enter.
Vim Save File

The file is written to disk and you remain in the editor.

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.

  1. Press Esc to switch to normal mode.
  2. Type :wq and press Enter.
Vim Save and Quit

The shortcut ZZ (uppercase, no colon) does the same thing: it saves the file and exits. This is the fastest way to save and quit.

The :x command is another alternative. The difference is that :x only writes to disk if there are unsaved changes, while :wq always writes and updates the file modification time.

Quitting Without Saving

To exit Vim without saving your changes, use :q!.

  1. Press Esc to switch to normal mode.
  2. Type :q! and press Enter.
Vim Quit Without Saving

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.

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:

Terminal
w !sudo tee % > /dev/null

This 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 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

The most important Vim commands for saving and quitting are :w (save), :wq (save and quit), and :q! (quit without saving). For a quick save and exit, use ZZ.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

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