The two commands you need to remember

To save your work and close Vim, press Escape first, then type :wq and press Enter. The colon tells Vim you are typing a command. The w means write (save). The q means quit (close). Together, :wq saves whatever you have typed and exits the program.

If you want to save without closing Vim, type :w instead. If you want to close without saving, type :q! — the exclamation mark forces Vim to quit even though you made changes. Without the exclamation mark, Vim will refuse to close and remind you that you have unsaved work.

The reason Vim works this way is historical: it was designed in the 1970s for terminals where the keyboard had no arrow keys or mouse. The colon-command system let you do everything without leaving the home row of keys. Modern Vim still uses the same system because it is fast once you know it.

Key Takeaways

  • Press Escape, then type :wq and press Enter to save your file and close Vim in one step.
  • Type :w alone to save without closing, or :q! to close without saving.
  • Vim will not let you close unsaved work unless you use the exclamation mark, which prevents accidental data loss.
  • The colon at the start of a command tells Vim you are typing an instruction, not text to insert into your document.

Why Vim makes you press Escape first

Vim has two modes: insert mode, where you type text into your document, and command mode, where you type instructions to Vim itself. When you open Vim, you start in command mode. If you press i, you enter insert mode and can type normally. When you are done typing, you press Escape to go back to command mode so Vim knows the next keys are commands, not text.

This separation exists because Vim needs to know whether you mean to type the letter w into your document or to execute the save command. If Vim were always listening for commands, you could never type the letter w without accidentally triggering something. By making you press Escape first, Vim knows that :w is a command, not part of your document.

Many people find this confusing at first because most modern text editors (like Notepad or Google Docs) do not have modes. They listen for keyboard shortcuts like Ctrl+S to save, but they let you type normally the rest of the time. Vim trades that convenience for speed: once you memorize the commands, you never have to reach for the mouse or use multiple keys at once.

What to do if you are stuck in Vim

If you opened Vim by accident and do not know how to close it, press Escape once, then type :q! and press Enter. The exclamation mark tells Vim to close without saving, so you will not lose anything you did not mean to create. This works even if you typed something by mistake.

If you are in insert mode and cannot figure out how to get back to command mode, press Escape. You may need to press it more than once. Once you see the cursor stop blinking or the mode indicator at the bottom of the screen change, you are back in command mode and can type :q! to exit.

If you have a file open and want to close Vim without saving the changes you made, use :q!. If you want to save the changes, use :wq. Vim will not let you close with just :q if you have unsaved changes — it will show an error message telling you to use the exclamation mark or save first.

Common commands beyond save and quit

Once you know :w and :q, you may want to learn a few other commands. Type :e filename to open a different file. Type :set number to show line numbers on the left side of the screen. Type :set nonumber to hide them again. Type :help to open Vim's built-in documentation.

You can also combine commands. For example, :wq filename saves your current file with a new name and closes Vim. Type :w filename to save a copy under a new name without closing. These are less common, but they save time if you need to work with multiple versions of the same file.

Why you might be using Vim without realizing it

Vim is often the default text editor on Linux and Mac computers when you use the command line. If you type a command like git commit without extra options, Git may open Vim automatically and ask you to type a message. If you type crontab -e to edit a scheduled task, the system may open Vim. If you are editing a file on a remote server over SSH, Vim is often the only editor available.

This is why learning :wq is useful even if you do not plan to use Vim regularly. You may end up in it by accident, and knowing how to save and exit means you can get out quickly without losing work or breaking a workflow.

The difference between :wq and :x

Both :wq and :x save and close Vim, but they work slightly differently. :wq always writes the file to disk, even if you have not made any changes. :x only writes if you actually changed something. For most people, this difference does not matter — both commands do what you want.

Some people prefer :x because it is one character shorter. Others prefer :wq because it is clearer what is happening: write, then quit. Either one is correct. If you are learning Vim, pick one and stick with it until it becomes automatic.

Frequently Asked Questions

What if I press :q and Vim says "No write since last change"?

Vim is telling you that you made changes but did not save them. Type :wq to save and close, or :q! to close without saving. If you want to save first and then close in two steps, type :w, press Enter, then type :q and press Enter again.

Can I undo changes after I close Vim?

No. Once you close Vim with :q!, the changes are gone. If you used :wq, the changes are saved to the file permanently. If you made a mistake and closed without saving, you cannot get the work back. This is why Vim refuses to close unsaved files unless you use the exclamation mark — it is protecting you from losing work by accident.

Is there a way to save without typing a colon?

Not in standard Vim. Some systems or custom configurations may add shortcuts, but the colon-command system is how Vim works by design. Learning the commands is faster than looking for alternatives once you use Vim regularly.

What does the tilde (~) at the end of a filename mean?

The tilde is a backup file that Vim creates automatically. If you edit a file called notes.txt, Vim may create notes.txt~ containing the previous version. You can delete these backup files safely — they are just Vim's way of protecting you if something goes wrong during editing.