FUNDAMENTALS A Complete Guide for Beginners
To close the Vim text editor, follow the below commands:
- To save and close the Vim editor, type
:wq!
and press ENTER from the insert mode. - To exit forcefully without saving, type
:q!
and press ENTER from the insert mode.
Now let’s discuss each of these ways in detail:
1. How to Exit and Save Files in Vim?
To save the file and exit the Vim editor simultaneously, execute :wq!
from the normal mode. For that, first, let’s open a file using the Vim editor. Then do some editing in the file from insert mode. After that, switch to the normal mode by pressing ESC. Finally, type :wq!
and press the ENTER button: The below image shows the file after reopening in Vim:
The process of saving the file and exiting the Vim editor was completed successfully.
2. How to Exit Vim Forcefully Without Saving File?
To forcefully exit the Vim editor without saving the file, execute :q!
from the normal mode. It discards all changes in the file since the last save and exits the Vim editor. Here, I will edit a file in Vim and then forcefully exit the editor without saving the changes:Now, I will execute :q!
to forcefully exit the Vim editor without saving the made changes: The image shows that upon reopening the file in Vim, any modifications made were discarded, leading to a forceful exit without saving the changes.
More Command Options to Close Vim in Linux
Here are some command options to exit the Vim text editor in Linux:
Command | Explanation |
---|---|
ESC |
Switches to command mode. |
:w |
Writes out the changes. |
:q |
Exits Vim. |
:q! |
Exits Vim forcefully discarding any changes. |
:wq |
Saves the changes and exits Vim. |
:x |
Saves the changes made and exits Vim. |
How to Exit Vim Insert Mode?
To exit the Vim insert mode, simply press the ESC button. Here I have opened a file in Vim and edited the file:The image shows the insert mode.
Now, press the ESC key to exit the insert mode:The image shows that Vim is now in normal mode exiting the insert mode.
Below, I will show you some other approaches on how to exit the Vim text editor insert mode:
1. Using “Ctrl-C”
To exit Vim insert mode, press Ctrl+c. It will exit the insert mode. Here, I have opened a file in Vim and done some editing while being in the insert mode:The image shows that the insert mode is on.
Now, I will press Ctrl+C to exit the insert mode:The image shows the situation after exiting the insert mode. This process is not exactly equivalent to pressing the ESC key as it does not trigger the InsertLeave auto-command. As a result, some related plugins or settings might not work properly.
2. Using “Ctrl-O” Followed by a Normal Mode Command
To exit insert mode temporarily, press Ctrl+o. It will temporarily exit the insert mode and execute a command from normal mode then switch back to the insert mode. For that, first, open a file in Vim. Then, press Ctrl+o to exit the insert mode temporarily. After that, execute :w!
. Executing the command, the Vim will switch to the insert mode automatically:The image shows that the Vim editor has exited the normal mode and is back on insert mode after executing the :w!
command.
Note: This shortcut is not suitable if you want to execute multiple commands in normal mode. If you want to execute multiple commands, you have to press Ctrl+o before every command.
3. Using “Ctrl-[“
Another alternative to pressing the ESC button is pressing Ctrl+[
to switch to the normal mode from insert mode. It is comparatively convenient to press since these two keys are adjacent to the home row or keyboard. On the other hand, pressing two keys simultaneously can be troublesome for some users.
Common Troubleshooting Cases While Exiting Vim
In this section, I have listed some troubleshooting commonly faced by Vim editor users:
1. What if I don’t want to enter Command-line Mode?
To exit the Vim editor without entering the command-line mode, press zz
. It will exit the Vim editor. This command is similar to :x
. For pressing zz
you do not need to enter the command line mode.
2. What if :wq failed?
The :wq
command might fail if the file is only in the read-only mode or the file name is missing. In any of these cases, :wq!
will not execute. In this circumstance, save the file on another file that has “write permission”. Then edit the file whatever is necessary. After that save and exit the Vim editor by executing :wq!
.
3. What if I am lost and have to force-quit?
To recover the lost changes, you will find a swap file with having extension .swap
. First, open this file with the Vim editor. Then you will be prompted to press R to recover that file. If you press R, the lost changes of your intended file will be recovered.
Conclusion
This article is a complete guide for the Vim users to close the Vim editor in various scenarios. I have demonstrated each of them in detail. Please put your valuable insights in the comment box.
People Also Ask
How to Exit Vim?
To exit the Vim editor, type :wq!
and press the ENTER button from the normal mode. It will save the file and exit the Vim editor.
How to start editing in Vim?
To start editing in Vim, press the i key to switch to the insert mode. In insert mode, you can edit the text inside the file according to your necessity.
How to exit Vim in terminal without saving?
To exit Vim in the terminal without saving, type :q
and press the ENTER button. It will exit the Vim editor without saving the file.
What can be done in insert mode?
In Vim insert mode, you can insert text into the file and do the necessary editing according to your choice.
What are most used modes in Vim?
There are three most used modes in Vim text editor. These are:
- Normal Mode: This is the initial mode of the Vim editor. Users can edit files, select text, execute commands, etc from this mode.
- Insert Mode: Allows you to edit your file, and move the cursor wherever you want.
- Command Line Mode: Allows users to execute various commands. To enter the command line mode, press the colon (:) key from the normal mode.