vi quit and exit tutorial

vim quit/save/exit FAQ: How do I quit/exit vim?

Answer: This depends by what you mean by the word exit. Here's a short list of the different ways I normally quit or exit a vi/vim editor session.

1) vi exit - no changes made to your file (vim quit command)

If you haven't made any changes to your file you can just quit your vi (or vim) editing session like this:

:q

Because vim tracks changes made to your file, you can use this simple vim quit command when you haven't changed your file, and it won't prompt you with any sort of "Are you sure?" message.

2) vim exit and discard changes

If you have made changes to your file, but you don't want to save them (you want to discard them), use the "vim quit dammit" command:

:q!

(I didn't make up the "dammit" part of that command, it's been referred to that way longer than I've been using vi.)

I don't know if it will help to think of it this way, but a friend of mine also refers to this as a "force quit" command.

3) vi/vim exit, save changes

Finally, if when you say "exit vim", you mean that you want to save your changes before you exit vim, there are at least two ways to save your changes to a file and then close the vi editor.

If you prefer being consistent and using only the vim last line mode you can issue this command to "write your changes to disk, and then quit (write and quit)":

:wq

In my case, I just like to go fast, so I issue this equivalent command from the vi command mode to write and quit:

ZZ

I think the only difference between those two commands is that the first command is issued from last line mode (and it takes me longer to type), while the second command is issued from vi command mode.

Other vim quit/exit/save thoughts

If you use these vim exit/quit/save commands while editing just one file at a time, all of these commands will work as shown above. If you are editing multiple files at one time with vi there are a few subtle differences with these commands (because vi needs to know what you mean by "exit" when you're editing several files at once). I'll be glad to write more about these differences if someone is interested, but until then, I'll leave this vi tutorial as is.

Post new comment

The content of this field is kept private and will not be shown publicly.