How to exit vim
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
- Vim uses distinct modes for different operations, including Insert mode (for typing) and Normal mode (for commands).
- The colon (`:`) command initiates Ex commands, which are powerful text editing operations.
- `:q` is the command to quit, `:w` is the command to write (save), and `!` typically forces an action.
- Combining `:w` and `:q` results in `:wq`, the common command to save and quit.
- `:q!` is the command to quit without saving, discarding any unsaved changes.
Overview
Vim, a highly configurable text editor, is renowned for its efficiency and power, especially among developers and system administrators. However, its modal nature and command-line interface can present a steep learning curve for new users. One of the most common initial hurdles is simply figuring out how to exit the editor. Unlike most graphical text editors where you click a close button or select 'File > Exit', Vim requires specific commands.
Understanding Vim's modes is crucial for navigating it effectively. The primary modes are Insert mode, where you can type text as you would in a standard editor, and Normal mode, which is the default mode when you open Vim. In Normal mode, keystrokes are interpreted as commands rather than text input. This distinction is fundamental to exiting Vim, as you must first be in Normal mode to issue exit commands.
Details on Exiting Vim
Entering Normal Mode
Before you can issue any command in Vim, including exit commands, you need to ensure you are in Normal mode. If you've been typing text and are currently in Insert mode, press the Esc key. You might need to press it a couple of times to be absolutely sure you've exited Insert mode. You can usually tell you're in Normal mode because your keystrokes won't immediately appear as text on the screen.
Saving and Exiting (:wq)
The most common way to exit Vim while ensuring your work is saved is to use the `:wq` command. Here's how:
- Press Esc to ensure you are in Normal mode.
- Type a colon (
:). This will move your cursor to the bottom-left corner of the screen, where you'll see the colon appear, indicating you are about to enter an Ex command. - Type
w. This command stands for 'write', which means 'save'. - Type
q. This command stands for 'quit'. - Press Enter.
Together, `:wq` tells Vim to write (save) the current buffer and then quit the editor. If the file has not been saved before, Vim will prompt you for a filename.
Exiting Without Saving (:q!)
Sometimes, you might make changes that you don't want to keep, or you might have accidentally opened a file. In such cases, you need to exit Vim without saving any modifications. The command for this is `:q!`.
- Press Esc to ensure you are in Normal mode.
- Type a colon (
:). - Type
q. - Type an exclamation mark (
!). The exclamation mark often signifies a 'force' operation in Vim, meaning it will perform the action regardless of the current state (like unsaved changes). <0x80><0x8B> - Press Enter.
This command tells Vim to quit immediately, discarding all changes made since the last save. Use this command with caution, as any unsaved work will be lost permanently.
Quitting Without Saving if No Changes Were Made (:q)
If you opened Vim, made no changes, and simply want to exit, you can use the `:q` command. This works just like `:q!` but will only succeed if there are no unsaved changes. If you try to use `:q` with unsaved changes, Vim will prevent you from exiting and display a message like 'E37: No write since last change (add ! to override)'.
- Press Esc to ensure you are in Normal mode.
- Type a colon (
:). - Type
q. - Press Enter.
Saving Without Exiting (:w)
You might also want to save your changes but continue editing the file. The command for this is simply `:w`.
- Press Esc to ensure you are in Normal mode.
- Type a colon (
:). - Type
w. - Press Enter.
This will save the current state of the file. You can then continue editing or issue other commands.
Saving to a New File (:w )
Vim also allows you to save the current buffer to a different file name using the write command. This is useful for creating a copy or starting a new version of your file.
- Press Esc to ensure you are in Normal mode.
- Type a colon (
:). - Type
w new_filename.txt(replacenew_filename.txtwith your desired name). - Press Enter.
This saves the content to the specified new file, leaving the original file open in Vim.
A Note on Vim Variants
While these commands are standard across most Vim installations, some environments might have slightly different configurations or include Vi, a predecessor to Vim. The core commands for exiting, however, remain consistent. The key takeaway is mastering the transition from Insert mode to Normal mode via the Esc key and then using the colon (:) to access Vim's powerful command-line interface.
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 documentation: 01.04 - Vim is not an editorVim License (similar to MIT)
- Vim Tips Wiki - Saving and QuittingCC-BY-SA-3.0
Missing an answer?
Suggest a question and we'll generate an answer for it.