Commands and Shortcuts to Exit Vim

Vim is an advanced version of the “Vi” editor for Linux and other OSs (Windows, macOS). It is suitable for the usual text editors and developers as it offers syntax highlighting features alongside multi-language support.

Vim is not like the traditional editors, i.e., you would press the “CTRL+S” or “CTRL+X” to save and exit or just exit the editor. You need to use the commands or the shortcut keys to exit the Vim editor and today’s guide will provide all the relevant commands/shortcuts to exit Vim.

The practical demonstration is performed on the Ubuntu 24.04 Noble Numbat. However, the commands and the shortcuts are applicable across all Linux distributions. 

Outline:

Commands to Exit Vim

Vim’s working depends on the mode which you are using, i.e., Insert, Visual, Command, etc. The commands only work when you are in the command line mode. Here’s how it actually works:

  • First, enter the normal mode using the “Esc” key (Normal mode is also known as command-line mode) and press the colon “:” to use the commands.

For instance, my Vim is in the “Insert” mode, as it is shown on the footer:

Image 536

Press the “Esc” key to navigate to the normal mode:

You can see there is no mode shown, which means you are in the normal mode. Now, if you press “:”, you will be able to execute the Vim commands:

Let’s have a look at the commands to exit Vim.

:q or :quit | Exit Vim If No Changes Occurred

If there are no changes made in the file, you can exit out of the editor using the “:q or :quit” commands:

:q
:quit

:q! or :quit! | Exit Vim Forcefully Without Saving the Changes

If you are unable to exit out of Vim normally, you can append the “!” with the “:q” or “:quit” commands to exit Vim forcefully without saving the changes:

:q!
:quit!

:wq | Save the Changes and Exit Vim

The “:wq” command stands for “Write” and “Quit”. It is used to save the changes and exit the Vim editor. Here is the demonstration:

:wq

:wqa | Write and Exit All Files Opened With Vim

If you have opened multiple files with Vim, you can write and close all these at once using the command:

:wqa

:x, :exit, :exit!, or :xit | Save the Changes and Exit Vim

If you want to save the changes and exit out of the editor, you need to use one of the following commands:

😡
:exit
:exit!
:xit

Forceful Exit Vim | Append “!” With Any of Vim Command

If we use the exclamation mark “!” at the end of the command (like “:q!”), you will be using that command in the forceful mode (applicable for most of the Vim commands). Thus, you can use any of the exclamation marks (!) with any of the above-listed commands to forcefully use them.

Shortcut Keys to Exit Vim

Shortcuts are also executed if you are in the normal mode. Thus, first, press the “Esc” key and then use the following shortcuts:

Save the Changes and Exit the Vim

Use the following key combination to save the changes and exit Vim:

Shift + ZZ

Exit the Vim

To just exit Vim, you can use the following shortcut key: 

Shift + ZX

Tip: 3 Ways to Exit the Read Only Vim

Have you ever got this error while using Vim “readonly option is set (add ! to override)”? 

This is because you are in the read-only mode and you won’t be allowed to exit out of the Vim normally. There are a few commands (as provided below) to exit the Read-Only Vim.

  1. Make the File Writable Forcefully and Exit Vim

Vim needs to be rolled out of the read-only mode. For that, use the “:write!” command which puts your Vim into write mode (forcefully):

:write!

Once you are able to write, now you can use any of the commands or the shortcuts to exit Vim.

  1. Enable the Write Option Normally and Exit Vim

There are multiple ways to open the file in read-only mode with Vim. One of which is using the following command:

vim -M <filename>

Now, when we tried to modify/edit the file, the following error occurred: 

Here are the ways to fix this error “E21: Cannot make changes, ‘modifiable’ is off”:

Step 1: Set the Modifiable Permission

Step 2: Set the Write Permissions

You can now exit Vim but forcefully. To get the normal exit permissions, you need to make it writable, using the command:

:set write

Now, you can exit the Vim using the commands or the shortcuts.

  1. Exit Forcefully

Apart from just making the file writable forcefully, you can directly exit the vim forcefully, even in the read-only mode. This is not the recommended solution, however, if you are stuck at all, you can use one of the forcefully exit commands:

:q!
:quit!
:exit!

That’s how you can exit Vim on Linux.

Bottom Line

Vim is a state-of-the-art text and code editor supported on Linux distributions and other OSs. To exit Vim, use one of the commands, i.e., “:q”, “:quit”, “:q!”, “:quit!”, “:wq”, “:x”, “:exit!”, or “:xit”. There are a few shortcut keys as well, i.e., “shift+zz” and “shift+zx”. 

All these commands and shortcuts are briefly explained. Each of the commands has its specific purpose to serve which you can read out in this post.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×