“Vim” is an open-source, well-known text editor beneficial for creating text files, writing source code, managing configuration files, and much more. It has enhanced features and a user-friendly environment and is also known as the “Vi Improved”.
It is available in almost all Linux distributions by default. Its usage is quite simple and easy. However, some people are not familiar with it. Keeping this in view, this post pens down the 10 possible ways to exit the vim editor with the following highlights:
- Prerequisites: Enable COMMAND Mode
- Exit Vim Editor Quickly
- Exit Vim Editor Forcefully (Without Saving)
- Exit All With Errors (Alternative of :q!)
- Confirm Before Exit
- Save and Exit
- Save and Exit (Alternative)
- Exit All Files
- Exit All Files Without Saving
- Save and Exit All Files
- Exit Vim Using Shortcut Keys
Prerequisites: Enable COMMAND Mode
The “Vim” editor contains two modes, i.e., “COMMAND”, and “INSERT”. In this scenario, the “Extra.txt” file is opened on the Vim text editor in the “INSERT” mode with the following content:
$ vim Extra.txt
Switch from “INSERT” to “COMMAND” mode by just pressing the “Esc” key:
Now the vim editor is in the “COMMAND” mode.
1. Exit Vim Editor Quickly
To simply and quickly exit the “vim”, type the “:q” as a command and hit the “Enter” key. It will appear on the bottom of the terminal as shown in the image:
:q
The “vim” editor will definitely be closed. Exit from the terminal without saving any change in the documents.
2. Exit Vim Editor Forcefully (Without Saving)
Type the “:q” with an exclamation mark “!” to exit the “vim” editor without saving the new changes made in the opened file:
:q!
The above command will discard all the changes and exit the editor.
3. Exit All With Errors (Alternative of :q!)
The user can also use the “:cq” command to exit, including all the upcoming errors in the “vim” editor:
:cq
4. Confirm Before Exit
To show the confirmation message before exiting the editor use the “conf(confirm)” keyword with the “:q” command in the following way:
Type the “(Y) for Yes” if you want to save the changes while “(N) for No”, and “(C) for Cancel”
5. Save and Exit
Type the “w(write)” for saving new changes with the “:q(quit)” in the editor and press the “Enter” key:
:wq
It will save the changes in the file and exit the vim editor at the same time.
6. Save and Exit (Alternative)
Here is an alternative keyword “x” used with the “:(colon)” in the form of a command to save and exit a file from the vim editor:
:x
It will also save the changes made in the desired file and exit the vim editor.
7. Exit All Files
Once all the user’s activities regarding the file are completed, then use the “a(all)” with the “:q” keyword to exit all the files at the same time and close the vim editor:
:qa
OR
:qall
The above command exits all the files in one shot.
8. Exit All Files Without Saving
If the user wants to forcefully quit all the files then use the “!(exclamation mark)” with the “:qa” command in this way:
:qa!
9. Save and Exit All Files
To save all the files and exit the vim editor run the “:wqa” or the “wqall” command. In this command, the “w” corresponds to the “write” keyword used to save the changes:
:wqa
10. Exit Vim Using Shortcut Keys
Apart from the commands, the user can also exit the vim editor using shortcut keys that are defined below:
- Shift+Z+Z: It is used to save the file and exit the vim editor from the terminal.
- Shift+Z+Q: It is used to exit the vim editor without saving the file:
These shortcut keys will definitely perform their specific jobs.
Conclusion
To exit the vim editor in Linux, use the “:q”, “:q!”, “:conf q”, “:wq”, and “:x
” commands. The “:qa”, “qa!”, “wqa”, and “cq” commands can also be used to perform this task. These commands perform their specified actions in a good manner. Before the execution of all of these commands, the “COMMAND” mode of the vim editor should be enabled. This post has provided the 10 possible ways to exit the vim editor.