How to exit vi

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: To exit Vim (or Vi), you first need to enter command mode by pressing the `Esc` key. Then, type `:q` to quit if you haven't made any changes, or `:wq` to save your changes and quit. If you want to quit without saving changes, use `:q!`.

Key Facts

Overview

Vim, and its predecessor Vi, are powerful, highly configurable text editors that are ubiquitous on Unix-like operating systems. While they offer immense flexibility and efficiency for experienced users, their modal nature can be a barrier for newcomers. Understanding how to exit Vim is often one of the first hurdles users face. Unlike most graphical text editors with obvious 'Save' and 'Exit' buttons, Vim requires specific commands to perform these actions.

Understanding Vim's Modes

Vim operates in different modes, the most common being Normal mode and Insert mode. In Insert mode, you can type text directly into the file, similar to a standard text editor. Normal mode, however, is where you issue commands. When you first open Vim, you are usually in Normal mode. To type text, you press keys like `i` (insert before cursor), `a` (append after cursor), or `o` (open new line below). To return to Normal mode from Insert mode, you press the Esc key.

Exiting Vim: Common Commands

Once you are in Normal mode (after pressing Esc), you can enter commands by typing a colon (:). This brings up a command-line prompt at the bottom of the screen. Here are the most common commands for exiting Vim:

1. Quitting Without Saving (if no changes) - `:q`

If you have opened a file and made no modifications, or if you have already saved your changes, you can simply exit by typing :q and pressing Enter. Vim will close the file and return you to your shell prompt.

2. Saving and Quitting - `:wq`

This is perhaps the most frequently used command for saving work. Type :wq and press Enter. The w stands for 'write' (save) and the q stands for 'quit'. This command will save all the modifications you've made to the file and then exit Vim.

Alternatively, you can use :x which also saves and quits, but only writes the file if it has been modified. This can be slightly more efficient in some scenarios.

3. Forcing a Quit Without Saving - `:q!`

If you have made changes to the file and Vim prevents you from quitting with :q (because it wants to prevent accidental data loss), you can use :q!. This command tells Vim to discard all unsaved changes and exit immediately. Use this command with caution, as any work not previously saved will be lost permanently.

4. Saving Without Quitting - `:w`

While not an exit command, it's useful to know that typing :w and pressing Enter will save your current changes without exiting Vim. This is handy if you want to periodically save your progress before continuing to edit.

A Step-by-Step Example

  1. Open a file using Vim: vim filename.txt
  2. Make some edits (e.g., type some text).
  3. Press the Esc key to ensure you are in Normal mode.
  4. To save and exit: type :wq and press Enter.
  5. To exit without saving (if you've made changes): type :q! and press Enter.
  6. To exit without saving (if you haven't made changes): type :q and press Enter.

Mastering these basic exit commands is crucial for navigating Vim effectively. As you become more familiar with Vim, you'll discover a vast array of commands that make it an incredibly powerful tool for software development and text manipulation.

Sources

  1. Vim (text editor) - WikipediaCC-BY-SA-4.0
  2. Vim Documentationfair-use

Missing an answer?

Suggest a question and we'll generate an answer for it.