The three ways to close Vi, and which one to use

Vi closes in three ways: save and exit, exit without saving, or suspend the program to come back later. The method you choose depends on whether you want to keep the changes you made to the file. If you are new to Vi, the most common reason people get stuck is that they try to close it the way they would close other programs — by pressing Ctrl+C or clicking an X button. Neither works in Vi.

Vi operates in two modes: command mode, where keystrokes perform actions like saving or moving the cursor, and insert mode, where keystrokes type text into the file. You must be in command mode to close the program. If you are typing text, press Escape first to return to command mode, then use one of the three methods below.

Key Takeaways

  • Press Escape to enter command mode, then type :wq and press Enter to save your changes and close Vi.
  • Type :q! and press Enter to close Vi without saving any changes you made.
  • Type :w and press Enter to save without closing, so you can keep working in the file.
  • If you need to step away temporarily without closing, press Ctrl+Z to suspend Vi and return to your command prompt, then type fg to resume where you left off.

Save and exit: :wq

This is the most common way to close Vi. Press Escape to make sure you are in command mode, then type a colon followed by the letters wq — the colon tells Vi you are entering a command, w means write (save), and q means quit (close). Press Enter. Vi will save all changes to the file and close when ready.

You will see the colon and your command appear at the bottom of the screen as you type. This is normal. Once you press Enter, the command executes and Vi closes. You will be back at your command prompt.

Exit without saving: :q!

Use this when you have made changes you do not want to keep. Type a colon, then q! — the exclamation mark tells Vi to force quit without saving. Press Enter. Vi will close and discard all changes since you last saved.

If you try :q without the exclamation mark and you have unsaved changes, Vi will refuse to close and show an error message. The exclamation mark overrides that safety check. Use it only when you are certain you want to lose your work.

Save without closing: :w

If you want to save your changes but keep working in the file, type a colon followed by w and press Enter. Vi will save the file and return you to editing mode. The file remains open and you can continue making changes.

This is useful if you have been working for a while and want to save your progress without closing the program. You can use :w as many times as you need while editing.

Suspend Vi temporarily: Ctrl+Z

If you need to step away from Vi and return to your command prompt without closing the file, press Ctrl+Z. This suspends Vi and returns you to the terminal. Your file remains open in memory.

To resume editing where you left off, type fg (foreground) at the command prompt and press Enter. Vi will reappear with your file exactly as you left it. This is different from closing — the file is not saved, but it is not closed either. You can suspend and resume as many times as you need.

What to do if you are stuck

If you are unsure what mode you are in or your keystrokes are not working as expected, press Escape several times. This returns you to command mode from anywhere. Then type the colon and your command.

If you see text appearing on the screen when you try to type a command, you are in insert mode. Press Escape first, then try again. If you see a message like "Not an editor command", you may have typed the command incorrectly — check that you included the colon at the start and that there are no extra spaces.

Frequently Asked Questions

What if I press Ctrl+C?

Ctrl+C does not close Vi. It may interrupt a command or search, but it will not exit the program. You must use :q, :q!, or :wq to close Vi properly.

Can I close Vi if I have not saved?

Yes, but only with :q!. If you use :q alone, Vi will refuse and show an error. The exclamation mark forces the close and discards unsaved changes.

How do I know if my file was saved?

After you use :wq, Vi closes and you return to the command prompt. The file is saved. If you use :q!, Vi closes without saving. If you are unsure, use :w to save first, then :q to close safely.

What does the colon at the start mean?

The colon tells Vi you are typing a command, not text. Commands in Vi always start with a colon when you are in command mode. Without it, Vi treats your keystrokes as text to insert into the file.

Can I close multiple files at once in Vi?

If you opened multiple files in Vi, :wq closes the current file and moves to the next one. To close all files at once, use :wqa (write and quit all) or :qa! (quit all without saving).