vi vim

How do I exit Vim?


I am stuck and cannot escape. It says:

type :quit<Enter> to quit VIM

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


  • 190

    Are you just trying to quit VIM ? If this is the case, press “escape” and then type ‘:q’

    – Pop

    Aug 6, 2012 at 12:28

  • 65

    Don’t forget the colon! You should type :quit and then hit the [ENTER] key.

    – Farahmand

    Mar 4, 2014 at 18:33

  • 141

    It’s really easy to learn the basics of vim, and it’s built right into your system. In terminal type “vimtutor”. 25 minutes later you will be going faster than your usual text editor!

    Jan 26, 2015 at 12:11

  • 11

    Check here more commands.

    – Toni

    Aug 7, 2015 at 15:36

  • 121

    To prevent git commit sending you to vim in the future: git config --global core.editor="nano"

    – Tom Kelly

    May 24, 2017 at 3:19


VIM has basically two modes: Command Mode (called also “Normal Mode” below) and Insert Mode.
Most likely you are in “insert mode” which does (not surprisingly) insert what you type, while in “command mode” it would try to execute the commands you give (such as :quit).

However VIM indicates when it is in insert mode by displaying “— INSERT —” on the left side of the last line (it also displays line and column number there).

Example VIM terminal screen

So to end insert mode and return to command mode, just hit the Esc key. 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.


  • 20

    Unless you have remapped esc or have a weird mapping in your .vimrc then it definitely should. If on linux type xev and make sure escape is the keytype you get when you hit escape.

    – dirvine

    Jun 11, 2014 at 23:49

  • 27

    Remember you can use ctrl+c if you can’t use Esc (like me because my shell is in TotalTerminal).

    Jan 27, 2015 at 15:12

  • 32

    :x == ZZ but :x != :wq. :x write file iff file has changed, :wq write file always (matter i.e. when using inotify).

    – Hauleth

    Feb 5, 2015 at 0:27

  • 54

    To be honest, I have a harder time using vim’s help system than using vim itself, and mostly rely on quick ref cards and online documentation.

    – bgvaughan

    Jul 8, 2015 at 6:40

  • 25

    if you don’t have permissions on the file but have sudo permissions :w ! sudo tee %

    – tvlooy

    May 23, 2017 at 18:53


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 afterwards.

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 file name 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, going to COMMAND mode, and then: to 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.


  • 16

    Thank you, the image is very helpful. However, for me w doesn’t change from Ex to Command mode, but Esc does. What am I doing wrong?

    May 28, 2017 at 5:24

  • 3

    If you write w-Enter that saves your file and goes back to COMMAND mode. I wrote it to have a full picture of save & quit commands.

    – Gergely

    May 28, 2017 at 5:35

  • 5

    oh, so you mean :w. Then it makes perfect sense. By the way, is there a command to reload from disk (that is, to revert changes but not close the file)? If so, it could be next to w in the diagram.

    May 28, 2017 at 9:50

  • 27

    What you’ve labeled command mode is actually normal mode. What you’ve labeled ex mode is actually command mode. Ex mode is a different beast altogether!

    – jpaugh

    Feb 12, 2018 at 23:35

  • 2

    @Gergely Well, I finally found the vim documentation: :help vim-modes.

    – jpaugh

    Feb 13, 2018 at 14:58


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.

This answer was inspired by the other one, originally authored by @dirvine and edited by other SO users. I’ve included more information from Vim reference, SO comments and some other sources. Differences for Vi and Vim are reflected too.