How to exit the Vim editor? [Answered]


I’m stuck and cannot escape. It says:

"type :quit<Enter> to quit VIM"

But when I type that it simply appears in the object body.

How to exit the Vim editor? Answer #1:

Hit the Esc key to enter “Normal mode”. Then you can type : to enter “Command-line mode”. A colon (:) will appear at the bottom of the screen and you can type in one of the following commands. To execute a command, press the Enter key.

  • :q to quit (short for :quit)
  • :q! to quit without saving (short for :quit!)
  • :wq to write and quit
  • :wq! to write and quit even if file has only read permission (if file does not have write permission: force write)
  • :x to write and quit (similar to :wq, but only write if there are changes)
  • :exit to write and exit (same as :x)
  • :qa to quit all (short for :quitall)
  • :cq to quit without saving and make Vim return non-zero error (i.e. exit with error)

You can also exit Vim directly from “Normal mode” by typing ZZ to save and quit (same as :x) or ZQ to just quit (same as :q!). (Note that case is important here. ZZ and zz do not mean the same thing.)

Vim has extensive help – that you can access with the :help command – where you can find answers to all your questions and a tutorial for beginners.

The difficulty of using Vim has been a running joke among programmers for years. To understand why, if you’re using a Mac or Linux computer, open the Terminal application. At the command prompt, type vim test.txt. That will create a text file, test.txt, which will open in Vim. Now try to close it and get back to the command prompt. If you can’t figure it out, just know that thousands of expert programmers who came before you couldn’t figure it out either.

Answer #2:

The first thing many programmers may do to try to get out of Vim is press control-c, or type quit, or type exit, or maybe exit(). None of these will work. If you press enough buttons, you might see the message jclancy saw: “type :quit<Enter> to quit VIM.” If so, you’ll probably try to type :quit and press enter, because you’re a perfectly reasonable person. But that won’t work either. As dirvine says in their answer to jclancy’s question, you have to hit the escape key first.

Pictures are worth a thousand Unix commands and options:

Enter image description here

I draw this to my students each semester and they seem to grasp vi afterward.

Vi is a finite state machine with only three states.

Upon starting, vi goes into COMMAND mode, where you can type short, few character commands, blindly. You know what you are doing; this isn’t for amateurs.

When you want to actually edit text, you should go to INSERT mode with some one-character command:

  • i: go to INSERT in the place of the cursor
  • I: go to INSERT mode at the beginning of the line
  • a: append after the cursor
  • A: append at the end of line
  • o: open a new line below the current line
  • O: open a new line in the place of the current line

Now, answering the question: exiting.

You can exit vi from EX mode:

  • q: if you haven’t made any modifications, or saved them beforehand
  • q!: ignores any modifications and quit
  • wq: save and quit
  • x: this is equal to wq

w and x accept a filename parameter. If you started vi with a filename, you need not give it here again.

At last, the most important: how can you reach EX mode?

EX mode is for long commands that you can see typing at the bottom line of the screen. From COMMAND mode, you push colon, :, and a colon will appear at the bottom line, where you can type the above commands.

From INSERT mode, you need to push ESC, i.e. the Escape button, go to COMMAND mode, and then: go to EX mode.

If you are unsure, push ESC and that will bring you to command mode.

The robust method is ESC-:-x-Enter which saves your file and quits.

Exit Vi editor – Answer #3:

Before you enter a command, hit the Esc key. After you enter it, hit the Return to confirm.

Esc finishes the current command and switches Vim to normal mode. Now if you press :, the : will appear at the bottom of the screen. This confirms that you’re actually typing a command and not editing the file.

Most commands have abbreviations, with optional part enclosed in brackets: c[ommand].

Commands marked with ‘*’ are Vim-only (not implemented in Vi).

Safe-quit (fails if there are unsaved changes):

  • :q[uit] Quit the current window. Quit Vim if this is the last window. This fails when changes have been made in current buffer.
  • :qa[ll]* Quit all windows and Vim, unless there are some buffers which have been changed.

Prompt-quit (prompts if there are unsaved changes)

  • :conf[irm] q[uit]* Quit, but give prompt when there are some buffers which have been changed.
  • :conf[irm] xa[ll]* Write all changed buffers and exit Vim. Bring up a prompt when some buffers cannot be written.

Write (save) changes and quit:

  • :wq Write the current file (even if it was not changed) and quit. Writing fails when the file is read-only or the buffer does not have a name. :wqa[ll]* for all windows.
  • :wq! The same, but writes even read-only files. :wqa[ll]!* for all windows.
  • :x[it]ZZ(with details). Write the file only if it was changed and quit, :xa[ll]* for all windows.

Discard changes and quit:

  • :q[uit]! ZQ* Quit without writing, also when visible buffers have changes. Does not exit when there are changed hidden buffers.
  • :qa[ll]!*, :quita[ll][!]* Quit Vim, all changes to the buffers (including hidden) are lost.

Press Return to confirm the command.

This answer doesn’t reference all Vim write and quit commands and arguments. Indeed, they are referenced in the Vim documentation.

Vim has extensive built-in help, type Esc:helpReturn to open it.

Answer #4:

This is for the worst-case scenario of exiting Vim if you just want out, have no idea what you’ve done and you don’t care what will happen to the files you opened.


This should get you out most of the time.

Some interesting cases where you need something like this:

  • iCtrlovg (you enter insert mode, then visual mode and then operator pending mode)
  • QappendEnter
  • iCtrlogQCtrlr=Ctrlk (thanks to porges for this case)
  • :set insertmode (this is a case when Ctrl\Ctrl-n returns you to normal mode)

This answer was corrected due to cases above. It used to be:


However, that doesn’t work if you have entered Ex mode. In that case you would need to do:


So a complete command for “I don’t want to know what I’ve done and I don’t want to save anything, I just want out now!” would be


Answer #5:

Vim has three modes of operation: Input mode, Command mode & Ex mode.

Input mode – everything that you type, all keystrokes are echoed on the screen.

Command mode or Escape mode – everything that you type in this mode is interpreted as a command.

Ex mode – this is another editor, ex. It is a line editor. It works per line or based on a range of lines. In this mode, a : appears at the bottom of the screen. This is the ex editor.

In order to exit Vim, you can exit while you are in either the ex mode or in the command mode. You cannot exit Vim when you are in input mode.

Exiting from ex mode

  1. You need to be sure that you are in the Command mode. To do that, simply press the Esc key.
  2. Go to the ex mode by pressing the : key
  3. Use any of the following combinations in ex mode to exit::q – quit :q! – quit without saving :wq – save & quit or write & quit :wq! – same as wq, but force write in case file permissions are readonly :x – write & quit :qa – quit all. useful when multiple files are opened like: vim abc.txt xyz.txt

Exiting from command mode

  1. Press the escape key. You probably have done this already if you are in command mode.
  2. Press capital ZZ (shift zz) – save & exit
  3. Press capital ZQ (shift zq) – exit without saving.

Hope you learned something from this post.

Follow Programming Articles for more!

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →