There is a famous joke in the programming world:
"How do you generate a random string? Put a web designer in front of Vim and tell them to save and exit."
This article is a translation of my original post. You can read the Chinese version here:
中文版:https://www.mfuns.net/article/113311
Although Vim is an incredibly powerful text editor, its operating logic is completely different from the Notepad or VS Code you might be used to.
If you are currently stuck in a terminal window, panic-typing Ctrl+C with no result, don't worry. This article will teach you how to save and exit Vim safely, from the b...
There is a famous joke in the programming world:
"How do you generate a random string? Put a web designer in front of Vim and tell them to save and exit."
This article is a translation of my original post. You can read the Chinese version here:
中文版:https://www.mfuns.net/article/113311
Although Vim is an incredibly powerful text editor, its operating logic is completely different from the Notepad or VS Code you might be used to.
If you are currently stuck in a terminal window, panic-typing Ctrl+C with no result, don't worry. This article will teach you how to save and exit Vim safely, from the basic :wq to the "geeky" shortcuts, helping you overcome your "Vim phobia" in 3 minutes.
Core Concept: The “ESC” Rule
Before learning any commands, you must understand one core concept of Vim: Modes.
Vim defaults to Normal Mode. In this mode, what you type on the keyboard is interpreted as commands, not text input. If you are currently typing text (Insert Mode), you must switch back to Normal Mode to execute save or exit commands.
The Golden Rule: No matter what you are doing, if you are unsure, press the Esc key a few times to ensure you are back in Normal Mode.
Scenario 1: Save and Quit
This is the most common scenario. You have finished editing the file and want to save your changes and close Vim.
Method 1: The Standard Command :wq
This is the most classic and well-known combination.
- Press
Esc. - Type
:wq. - Press
Enter.
Breakdown:
-
:Enters Command-line mode. -
wStands for Write (save). -
qStands for Quit.
Method 2: The Smart Command :x
This command is slightly smarter than :wq.
- Press
Esc. - Type
:x. - Press
Enter.
Difference between :x and :wq:
:wq forces a file write (updating the file's modification timestamp) even if you didn't change anything.
:x only writes to the file if the content has actually changed. For SEO or file auditing purposes, keeping accurate timestamps is often important, so getting into the habit of using :x is recommended.
Method 3: The Shortcut ZZ
If you want to show off your "geek" style, use this uppercase shortcut. No colon required.
- Press
Esc. - Hold
Shiftand pressztwice (inputZZ). - This is exactly equivalent to
:x.
Scenario 2: Quit Without Saving
Maybe you messed up the configuration file, or you just wanted to peek at a file but accidentally typed some random characters. You need to "discard changes and exit."
Method 1: Force Quit :q!
- Press
Esc. - Type
:q!. - Press
Enter.
Breakdown:
-
qQuit. -
!In Linux, this usually means Force. It tells Vim: "I don't care what I changed, get me out of here now!"
Method 2: The Shortcut ZQ
Just like ZZ, this is a faster way to force quit.
- Press
Esc. - Type
ZQ(Shift + z + q). - This is equivalent to
:q!.
Scenario 3: Save Only (Keep Open)
If you are writing a long piece of code or an article and want to save your progress to prevent data loss (e.g., power outage) but don't want to close the editor yet.
-
Command:
:w -
Action: Press
Esc, type:w, and pressEnter.
Advanced: Troubleshooting Common Errors
Here are solutions to the most common errors beginners face when trying to exit.
- Error:
E37: No write since last change
E37: No write since last change-
Reason: You modified the file but tried to exit using only
:q. Vim is protecting your data. -
Solution: * To save: Use
:wq.- To discard changes: Use
:q!.
- To discard changes: Use
- Error:
E45: ‘readonly’ option is set
E45: ‘readonly’ option is set-
Reason: You opened a file without write permissions (e.g., a system config file) but modified it.
:wqwill fail. -
Solution: If you have
sudoaccess, use this "magic command" to force save without closing and reopening:
:w !sudo tee %-
w: Write. -
!sudo: Run an external command with admin privileges. -
tee: Linux command to redirect input to a file. -
%: Represents the current filename in Vim.
(After pressing Enter, Vim might warn you that the file has been changed externally. Just press
Lto load the changes.) -
Summary: Cheat Sheet
Feel free to take a screenshot of this list!
| Goal | Command |
|---|---|
| Standard Save & Quit |
:wq or :x
|
| Fast Save & Quit |
ZZ (Uppercase) |
| Discard Changes & Quit | :q! |
| Fast Force Quit |
ZQ (Uppercase) |
| Save Only | :w |
Vim has a steep learning curve, but once you master these basic exit commands, you've already crossed the hardest threshold. I hope this guide helps you feel more confident using the terminal!