Exit Vim

Shawn Wang
InstructorShawn Wang
Share this video with your friends

Social Share Links

Send Tweet

Every programmer starting to work in a *nix environment eventually finds themselves in VIM, the default text editor. Often by mistake! Because VIM lacks the typical polished GUI of normal programs, it can be hard to discover the correct sequence of commands to even exit the program. Once you learn how to exit VIM, then the next task is to also learn how to make VIM do what you want -while- exiting!

In this lesson we cover 7 different ways of exiting VIM, whether you are in EX mode or Command (also known as Escape) mode, whether you are editing a single file or several at once.

Instructor: [00:01] Say you find yourself inside of Vim, and you want to exit. The first thing you want to try is pressing :q and then enter. That should take you out. Sometimes, you want to make changes to your files. Here, I'm making a new file, and I'm pasting in some text.

[00:18] You notice at the bottom left of the screen, I am in Insert mode. That's a different mode from where we were before, which is either the X or the Command modes. To exit out of Insert mode, I have to press Escape. Then when I try to press :q again, it doesn't let me because I've made some changes to the files. It's warning me I shouldn't do that.

[00:38] I can follow its instructions and just add an exclamation mark to override the warning and exit out of Vim. Then when I open up the file again, the data's gone because I didn't save it. What we can do in this case is press Escape and wq to save, as well as quit. When we go back to the original file, we can see that our changes have been saved.

[01:03] There is another option, where you can ask for confirmation about the changes. Let's say I make a change to the text here. I'm going to enter in, "Hello world." Then I exit, hit :conf q and enter. It asked me if I want it changed and if I want to save it. I can say, "Yes," and then the new text will be saved when I reopen the file.

[01:29] It's a common misconception that you have to be in X mode to quit. Here, I'm making a new file. I can press Shift-ZZ to save and quit, so that when I open it up again, the text persists. If you don't want your text to auto-save, you can always say Shift-ZQ.

[01:49] Here, I'm going to add new text. Then I hit Shift-ZQ and it exits. When I go back to the file, you see the text doesn't persist because it wasn't saved. Now you know how to exit in both Command mode with no colons and in X mode with colons.

[02:07] Sometimes, you'll be editing multiple files. In these situations, you will have multiple buffers to close. Just typing :q doesn't quite work because there's more files to close. You can keep typing :q to close them, but that gets a bit gnarly when you need to edit multiple files. Instead of typing :q multiple times, you can type :qa, and that will close all three files.

[02:34] One problem with doing :qa is it doesn't save changes for you. Here, I'm doing :qa. You can see the scary error messages from before. You'd have to know to save the file to exit. If, let's say, we don't want to do that, we can actually ask for confirmations just like we did before.

[02:54] Here, I'm going to add some text. I'm going to ask for confirmations as I quit all the files. Do I want to quit? Yes. That's how you exit Vim.