Introduction
Vim is a Linux text editor based on the older Vi editor. Learning how to exit Vim is important for developers, system administrators, and other IT professionals.
This guide will show you a few different ways to exit the Vim text editor.
Prerequisites
- A Linux system (this tutorial uses Ubuntu 22.04).
- Access to the terminal.
- Vim installed.
Exit Vim Using Esc Key
Vim operates in different modes, each with a distinct purpose and set of commands. To exit Vim, the editor needs to be in Normal mode. To switch to Normal mode, press the Escape key. When switching from Insert mode, you should see the ––INSERT––
label disappear from the lower-left corner.
Exit Vim without Saving Changes
If you haven't made any changes, take the following steps to exit Vim:
1. Press the Escape key.
2. Type :q
.
3. Hit Enter.
The system exists Vim and brings you back to the command line.
Exit Vim and Save Changes
To save changes and exit the editor, take the following steps:
1. Press the Esc key.
2. Type :wq
. The letter w
saves the file, and the letter q
exits the editor.
3. Hit Enter.
Note: If you want to personalize your Vim interface and add syntax highlighting, learn how to change and use Vim color schemes.
Exit Vim and Discard Changes
If you have made changes using Vim but want to discard them, take the following steps:
1. Press the Escape key.
2. Type :q!
. The q
stands for quit, and the !
forces Vim to discard any changes.
3. Hit Enter.
Conclusion
This article explained several ways to exit the Vim text editor, depending on whether you want to save or discard the changes.
Next, learn other essential Vim commands.