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
Key Facts
- The `Esc` key is used to enter command mode.
- `:q` quits without saving changes if no modifications have been made.
- `:wq` saves the current changes and quits the editor.
- `:q!` forces quitting without saving any modifications.
- Vim is a highly configurable text editor and a descendant of the Vi editor.
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
- Open a file using Vim:
vim filename.txt - Make some edits (e.g., type some text).
- Press the
Esckey to ensure you are in Normal mode. - To save and exit: type
:wqand pressEnter. - To exit without saving (if you've made changes): type
:q!and pressEnter. - To exit without saving (if you haven't made changes): type
:qand pressEnter.
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.
More How To in Daily Life
Also in Daily Life
More "How To" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- Vim (text editor) - WikipediaCC-BY-SA-4.0
- Vim Documentationfair-use
Missing an answer?
Suggest a question and we'll generate an answer for it.