How To Save A File In Vim / Vi And Quit The Editor - Linuxize
Có thể bạn quan tâm
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.
This guide explains how to save a file in Vim, quit the editor, and handle common situations like read-only files.
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:
Terminalvim 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 Esc to switch to normal mode.
- Type :w and press Enter.

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.
- Press Esc to switch to normal mode.
- Type :wq and press Enter.

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!.
- Press Esc to 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.
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:
Terminalw !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.
Quick Reference #
| 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 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 |
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.
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.
If you have any questions, feel free to leave a comment below.
Tags
vimLinuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
SubscribeUnsubscribe anytime. We respect your inbox.
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 pageTừ khóa » Vi Commands Exit
-
More Linux Resources
-
How To Exit (Quit) Linux Vim/Vi Editor - PhoenixNAP
-
How To Exit (quit) Linux Vi Editor With Or Without Saving Changes Step ...
-
How To Exit The Vi Or Vim Editor - How-To Geek
-
Quit The Vi Editor Without Saving Your Changes
-
How To: Vi / Vim Save And Quit The Editor Command - NixCraft
-
How To Exit Vim – Vim Save And Quit Command Tutorial
-
How Do I Exit Vim? - Stack Overflow
-
Saving Changes And Quitting Vi
-
How To Exit From Vi In Linux Code Example
-
How To Exit A File In Vi / Vim Editor In Linux - Tecmint
-
Basic Vi Commands - Colorado State University
-
How To Exit Vim? Multiple Ways To Quit Vim Editor - It's FOSS
-
10 Ways To Quit/Exit From Vim Editor In Linux/UNIX? Examples - Java67