FactorPad
Faster Learning Tutorials

Learn three ways to quit Vim

One of Vim's most frequently searched topics is how to close Vim. Many never return.
  1. Quit - If you did not edit a file and want to quit Vim type :q. Hit Esc first if you don't know your mode.
  2. Quit without saving - If you edited a file and want to quit without saving, type :q! from Normal Mode, or Esc :q! from any other mode.
  3. Quit and save - If you want to write or save changes and then exit Vim, hit Esc then :wq.
Paul Alan Davis, CFA, September 2, 2017
Updated: August 7, 2018
That's the quick answer. To learn about modes and resolve a common error keep reading.

Outline Back Next

~/ home  / tech  / vim  / reference  / exit vim


Exit Vim and Vi Editors With or Without Saving Changes to a File

Video Tutorial

If you prefer, this topic is also covered in video.

Exit Vim - Three ways to quit vim including with and without saving (6:18)

Videos can also be accessed from our Vim Reference Playlist on YouTube.

Video Script

Beginner

What makes the Vi and Vim editors unique is the concept of modes which makes editing fast once you gain a comfort level. If you are familiar with other text editors or word processors you will also notice that Vim lacks menus. So yes, it is very different.

Example 1 - Quit Vim From Normal Mode

In our first example, we'll open an existing text file using vim filename at the command line.

$ vim filename

By default, Vim opens files in Normal Mode. The Vim and Vi editors have several modes, but for now just remember this mode plus two others: Insert Mode (for editing) and Command Mode (to tell Vim what to do, like quit).

Note: learn faster by creating a file like we did here.
1 This is a two-line text file used for several illustrations 2 and saved as "filename". ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "filename" 2L, 85C 1,1 All

We can see information in the footer like "filename" for the name of the file, 2L 85C for the number of lines and characters. By default Vim opens the file in position row 1, column 1, so 1,1, and All is what portion of the file is showing.

Let's close this file using instructions from above. Since we are in Normal Mode we can simply type :q followed by the Enter key and you will have quit Vim and will return to the command prompt. Here, in Linux, it shows your last command and the cursor indicating that the system is patiently waiting for your next request, like any well-behaved computer.

$ vim filename $ _

Example 2 - Quit Vim Without Saving Changes

In our second example, we'll open that same text file again using the vim filename convention.

Now imagine you made a typo right away and inadvertenly pushed the s key. This erased the first letter of the file where the cursor was and switched you from Normal Mode to Insert Mode as shown below.

1 his is a two-line text file used for several illustrations 2 and saved as "filename". ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -- INSERT -- 1,1 All

Afraid that we may have done something permanently wrong (we haven't), let's close this file and quit without saving. Hit Esc, to go from Insert Mode to Normal Mode, then :q! going into Command Mode and telling Vim to quit without saving. You will return to the command prompt and it will likely look like this. Everything is okay, no damage done.

$ vim filename $ vim filename $ _

Example 3 - Edit, save changes and exit Vim (write and quit)

In our third example, we will open that same text file again by typing vim filename. This time we will edit the file intentionally. Below is how it looks unchanged.

1 This is a two-line text file used for several illustrations 2 and saved as "filename". ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "filename" 2L, 85C 1,1 All

Your cursor will be in row 1, column 1. Now to move down one row, or line, type j, and then capital A which does two things. First, it takes us to the end of the second line, and second it switches us from Normal Mode to Insert Mode as indicated by the -- INSERT -- in the bottom left section (see below). Now, we are finally ready to edit the file.

Now type (Wow, that's original!) as indicated below.

1 This is a two-line text file used for several illustrations 2 and saved as "filename". (Wow, that's original!) ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -- INSERT -- 1,1 All

Next, let's write and save these changes as instructed in step 3 above. Hit Esc to go from Insert Mode to Normal Mode. Notice how -- INSERT -- in the bottom left went away (assuming you have standard settings and are following along at home).

Now we can save, or write and quit with :wq. And because here you are writing before quitting you won't get an error message, and will return to the command line.

$ vim filename $ vim filename $ vim filename $ _

Vim Questions and Answers

Q:  What does the error message No write since last change (Add ! to override) mean?
A:  This is Vim's way of reminding you that you didn't save changes to an edited file. In this case decide if you want to keep the changes (use :wq) or not (use :q!).

Q:  Why are there three $ vim filename lines at the end of Example 3?
A:  Because we opened the same file three times. In Linux, if you'd like to clear the screen use the clear command.


Related Vim Commands and Procedures


What's Next?

We have hundreds of videos on YouTube. Subscribe now so you don't miss new opportunities to learn.

  • For links to all Vim Reference material, click Outline.
  • To learn more about how to open Vim, click Back.
  • To expand on saving Vim files, click Next.

Outline Back Next

~/ home  / tech  / vim  / reference  / exit vim



 
 
Keywords:
exit vim
exit vim Linux
quit vim
how to exit vim
how to quit vim
vim quit without saving
vim exit without saving
vim close buffer
close vim
vim q
exit vim editor