DEV Community

高雅的松灯
高雅的松灯

Posted on • Originally published at mfuns.net

How to Save and Exit Vim? The Ultimate Guide for Beginners

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.

  1. Press Esc.
  2. Type :wq.
  3. Press Enter.

Breakdown:

  • : Enters Command-line mode.
  • w Stands for Write (save).
  • q Stands for Quit.

Method 2: The Smart Command :x

This command is slightly smarter than :wq.

  1. Press Esc.
  2. Type :x.
  3. 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.

  1. Press Esc.
  2. Hold Shift and press z twice (input ZZ).
  3. 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!

  1. Press Esc.
  2. Type :q!.
  3. Press Enter.

Breakdown:

  • q Quit.
  • ! 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.

  1. Press Esc.
  2. Type ZQ (Shift + z + q).
  3. 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 press Enter.

Advanced: Troubleshooting Common Errors

Here are solutions to the most common errors beginners face when trying to exit.

1. Error: 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!.

2. Error: E45: 'readonly' option is set

  • Reason: You opened a file without write permissions (e.g., a system config file) but modified it. :wq will fail.
  • Solution: If you have sudo access, 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 L to 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!

Top comments (1)

Collapse
 
art_light profile image
Art light

This is a fantastic beginner-friendly guide—clear, reassuring, and genuinely practical. You explain Vim’s mental model (modes + Esc) in a way that immediately reduces anxiety, which is the hardest barrier for new users. The scenario-based structure and shortcuts like ZZ, ZQ, and the sudo tee trick turn common frustrations into confidence-building wins. This is exactly the kind of article that helps someone cross the “Vim is scary” threshold and start using it productively.