The quickest way out of vi

To close vi, press the Escape key once, then type :q and press Enter. If you have made changes to the file, vi will refuse to close and will show you a message saying "No write since last change". At that point, type :q! (with an exclamation mark) and press Enter to close without saving, or type :wq and press Enter to save your changes first, then close.

The reason vi behaves this way is that it has two modes: insert mode (where you type text) and command mode (where you give instructions like "save" or "quit"). You must be in command mode to close the file. Pressing Escape takes you out of insert mode and back into command mode, where vi will listen for the commands that tell it to exit.

If you are not sure which mode you are in, press Escape. It will not hurt anything — if you are already in command mode, nothing will happen. If you are in insert mode, you will return to command mode.

Key Takeaways

  • Press Escape, then type :q and Enter to close vi if you have not made any changes.
  • Type :wq and Enter to save your changes and close the file at the same time.
  • Type :q! and Enter to close without saving if you have made changes you want to discard.
  • If vi refuses to close, you are probably still in insert mode — press Escape and try again.

Understanding vi's two modes

Vi starts in command mode, where the keys you press are interpreted as instructions rather than text. When you want to type actual content into the file, you press i (for insert), a (for append), or o (for open a new line), and vi switches to insert mode. In insert mode, the characters you type appear in the file.

This two-mode design is why closing vi feels unintuitive if you are used to other text editors. In most modern editors, you just press Ctrl+Q or go to File > Exit. In vi, you have to remember that you are in a mode, switch out of it, and then give the exit command.

The status bar at the bottom of the screen will sometimes tell you which mode you are in — if you see "-- INSERT --" at the bottom left, you are in insert mode. If the status bar is empty or shows only the filename and line number, you are in command mode.

Saving changes before you close

The command :w saves the file without closing it. Type :w and press Enter, and vi will write your changes to disk. You can then continue editing, or you can close the file afterward with :q.

The command :wq combines both steps — it saves and closes in one action. This is the most common way to exit vi after you have finished editing. Press Escape, type :wq, and press Enter.

If you have made changes but want to close without saving them, use :q! instead. The exclamation mark tells vi to ignore the unsaved changes and close anyway. Vi will not ask you to confirm — it will simply close the file and return you to the command prompt.

What to do if vi will not close

If you type :q and vi shows the message "No write since last change", it means you have unsaved changes and vi is protecting you from losing them. You have three choices: save the changes with :wq, discard them with :q!, or go back to editing by pressing Escape and continuing your work.

If you type a command and nothing happens, you are probably still in insert mode. Press Escape and try the command again. A common mistake is typing :q while in insert mode — vi will simply insert the characters ":q" into your file instead of interpreting them as a command.

If you see a message like "Not an editor command", you may have typed the command incorrectly. Make sure you pressed Escape first, that you typed a colon before the command (like :q, not just q), and that you pressed Enter at the end.

Alternative ways to exit

Some versions of vi also respond to :x, which works like :wq — it saves and closes. However, :wq is more widely supported across different systems, so it is the safer choice if you are working on unfamiliar machines.

On some systems, you can also press Ctrl+D in command mode to close the file, but this does not work on all versions of vi and is not a reliable method. Stick with :q, :wq, or :q! for the most consistent results.

If you are using a variant of vi like vim (which stands for "vi improved"), the same commands work exactly the same way. Vim is installed on most modern Linux and Mac systems by default, and it behaves identically to vi for closing files.

Why vi works this way

Vi was written in 1976 for systems with very slow connections and limited screen space. The two-mode design was a way to minimize the number of keystrokes needed to edit a file — in command mode, single letters perform complex actions, which saves time on slow networks. The design has persisted because it is efficient once you learn it, and because vi is so widely installed on servers and Unix-like systems that changing it would break countless workflows.

Understanding this history does not make vi easier to use, but it explains why the interface feels so different from modern editors. If you find yourself using vi regularly, the commands will become automatic after a few weeks of practice.

Frequently Asked Questions

What if I accidentally closed vi and lost my work?

Vi creates a backup file (usually named with a dot and a tilde, like .filename~) in the same directory as the original file. You can open this backup file to recover your unsaved changes. However, this backup may not exist on all systems or all versions of vi, so it is not a reliable recovery method — always use :wq to save before closing.

Can I close vi without using the keyboard?

No. Vi is a keyboard-driven editor and does not respond to mouse clicks or menu commands. You must use the keyboard commands described above. If your keyboard is not working, you will need to restart your terminal or system to force vi to close.

What does the colon at the beginning of commands like :q mean?

The colon tells vi that you are typing an "ex command" — a command from the older line editor that vi is built on top of. In command mode, the colon moves the cursor to the bottom of the screen and opens a command line where you can type instructions. This is why you see the colon appear at the bottom when you type it.

Is there a way to undo closing vi if I used :q! by mistake?

No. Once you close vi with :q!, the file is closed and your unsaved changes are gone. There is no undo for closing the editor itself. If you realize you made a mistake immediately, you can reopen the file and use the backup file (if it exists) to recover some of your work, but this is not may provide to work on all systems.