Your Search Bar For Shrewd Tips

How To Close Vi


If you are new to the command line, closing the vi text editor can feel less obvious than closing a regular desktop app. Vi does not have a visible close button, and the commands for saving or discarding changes are entered through the keyboard. The good news is that once you know a few simple keystrokes, you can safely close vi in seconds.

This guide explains how to close vi with and without saving, what to do if the editor will not exit, and how to avoid losing changes. The instructions also apply to most versions of Vim, the popular editor based on vi. Whether you need to save a file, abandon edits, or quit without changing anything, use the steps below to choose the right command.

How to Close Vi: The Quick Answer

To close vi, first press the Esc key to return to Normal mode. Then type the command that matches what you want to do and press Enter:

  • :q — Quit if there are no unsaved changes.
  • :q! — Quit without saving your changes.
  • :wq — Save the file and then quit.
  • :x — Save changes, if there are any, and quit.
  • ZZ — Save changes and quit using a keyboard shortcut.

In vi, the colon opens the command line at the bottom of the screen. You must press Enter to run a colon command. The ZZ shortcut is different: press and release the uppercase Z key twice while in Normal mode. It does not require a colon or Enter.

Step One: Return to Normal Mode

Vi works in different modes. In Insert mode, your keystrokes enter text into the file. In Normal mode, keystrokes are interpreted as editing commands. If you try to type :q while still in Insert mode, those characters may be added to your document instead of closing the editor.

Press Esc before entering a quit command. If you are unsure which mode you are in, press Esc once or twice. This is safe: it does not delete your file or undo your edits. When you are back in Normal mode, type a colon command, such as :q, and press Enter.

How to Quit Vi Without Saving

To close vi and discard all unsaved changes in the current file, press Esc, type :q!, and press Enter. The exclamation mark tells vi to force the quit, even though the file has been modified since it was last saved.

Use this command carefully. Any changes made after the last save will be lost. The command does not delete the file from your computer; it only exits the editor without writing your recent edits to that file. If you have second thoughts, do not press Enter. You can remove the command from the command line with Backspace or press Esc to cancel it.

A typical sequence is:

  • Press Esc to enter Normal mode.
  • Type :q!.
  • Press Enter to quit without saving.

How to Save and Close Vi

If you want to keep your changes and close the editor, press Esc, type :wq, and press Enter. The w means “write,” which saves the file, and the q means “quit.” This is one of the most widely used commands for saving and exiting vi.

You can also use :x. It saves the file if it has been changed and then exits. If the file has not changed, vi can quit without writing it again. For most everyday editing, :wq and :x achieve the same practical result: your edits are saved and the editor closes.

Another option is the shortcut ZZ. Make sure you are in Normal mode, then press Shift+Z twice. Vi saves modified content and exits. Because this shortcut does not begin with a colon, do not type it into the command line.

How to Quit Vi When You Have Not Changed the File

If you opened a file but made no changes, press Esc, type :q, and press Enter. The plain quit command works when there are no unsaved edits. You can also use :wq or :x, although saving is unnecessary if the file is unchanged.

If vi says that there are unsaved changes, you may have edited something without noticing. Review the file before choosing whether to save. Use :wq to keep the edits or :q! to discard them. Do not use the force-quit command automatically if you are unsure whether the changes matter.

What to Do If Vi Will Not Close

The most common reason vi refuses to quit is that the file contains unsaved changes. When you enter :q, vi may show an error such as “No write since last change.” This message is a safeguard: the editor is warning you that quitting normally could leave your latest edits unsaved.

Choose one of these options based on what you want to do:

  • To save your edits and exit, enter :wq.
  • To discard your edits and exit, enter :q!.
  • To save the file but stay in vi, enter :w.

If the command seems to do nothing, check that you pressed Esc first and Enter after the command. A colon command should appear on the bottom line of the screen. If text is being inserted into the document, press Esc to return to Normal mode, then try again.

If you are editing several files, using split windows, or working in a Vim session with multiple buffers, a quit command may close only the current view or may be blocked by another unsaved file. In Vim, :qa quits all windows when there are no unsaved changes, while :qa! force-quits all windows and discards unsaved edits. Use the force version only if you are certain you do not need those changes. These “all” commands are Vim features and may not be available in every traditional vi implementation.

How to Save a File Before Closing

If you want to save without exiting, press Esc, type :w, and press Enter. Vi writes the current file to disk and remains open. Once the save is complete, you can close the editor with :q.

You can also save the contents under a different filename by entering :w newname.txt and pressing Enter. This writes a copy using the name you specify. To save that file and then exit, enter :wq newname.txt. Check the filename carefully before pressing Enter so you do not overwrite a different file by mistake.

If vi reports that it cannot write the file, the issue may involve permissions, a read-only file, or a directory that does not allow writing. Do not assume that adding an exclamation mark will resolve every permissions problem. In some cases, you may need to save to a different location or reopen the file with the appropriate permissions.

Vi Exit Commands at a Glance

  • :q — Close vi when there are no unsaved changes.
  • :q! — Close vi and discard unsaved changes.
  • :w — Save changes and remain in the editor.
  • :wq — Save changes and close vi.
  • :x — Save if needed, then close vi.
  • ZZ — Save changes and close vi from Normal mode.
  • :qa — In Vim, close all windows if there are no unsaved changes.
  • :qa! — In Vim, force-close all windows and discard unsaved changes.

The commands beginning with a colon must be followed by Enter. The ZZ shortcut is entered directly in Normal mode. Remembering these differences prevents one of the most common beginner mistakes.

Common Mistakes to Avoid When Closing Vi

One frequent mistake is pressing Ctrl+C and expecting it to close vi. Ctrl+C may interrupt certain actions, but it is not the standard way to save and exit. Use the appropriate vi command instead so you know whether your changes are being kept or discarded.

Another mistake is using :q! when you meant to save. The exclamation mark forces vi to quit and can discard your unsaved edits. If you want to preserve your work, choose :wq or :x. If you only want to make a save point and continue editing, use :w.

Finally, pressing Ctrl+Z in a Unix or Linux terminal usually suspends the editor process rather than closing it. The terminal may return to a shell prompt, but vi may still be running in the background. To exit normally, return to the editor if necessary and use :q, :wq, or :q! according to your needs.

Conclusion

Closing vi is straightforward once you know the difference between saving and discarding changes. Press Esc to return to Normal mode, then use :q to quit an unchanged file, :q! to discard edits, or :wq to save and exit. You can also use :x or the ZZ shortcut to save changes and close the editor.

When vi will not close, check for unsaved changes and read the message shown on the screen before choosing a command. With these basic vi exit commands, you can close the editor confidently while protecting the work you want to keep.


Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.

Shrewdnia

Shrewdnia

Shrewdnia is a destination for curious minds seeking clarity, knowledge, and informed perspectives. Through insightful articles and practical guides our passionate team explores a wide range of topics designed to help readers understand the world around them, make smarter decisions, and stay informed in an ever-changing landscape.


💡 Every question sparks discovery, and every perspective enriches the conversation. Share your thoughts and insights in the comments 👇

Back to blog

Leave a comment

JOIN THE SHREWDNIA COMMUNITY FORUM

What do you think?

Have an opinion, experience, or question about this topic? Join the Shrewdnia Forum and share your thoughts with other readers.

Join the Forum →