Vim Recipes Basics Quitting Vim

Quitting Vim

Problem

You've finished using Vim and now you want to close the program.

Solution

To save the changes in the current file then quit use :xThe more usual suggestion for saving then quiting is :wq. We use :x here because it only saves the file if it has been changed, thus preserving its timestamp and saving needless disk access.. In Normal mode you use ZZ.

Discussion

The way you quit Vim depends on what you want to quit (the whole program, or just the current window) and what you want to do with your unsaved changes.

As mentioned above, if you're using a single window either :x or ZZ will save any unsaved changes, and exit Vim.

To exit and discard your changes you use :q! (mnemonic: quit in a possibly dangerous (exclamatory) manner).

You can also quit on the condition that there are no unsaved changes with :q; if you do need to save Vim warns you E37: No write since last change (add ! to override).

If you're using multiple windows the above commands will act upon the current window. To quit all windows use :qa (mnemonic: quit all). Vim will prompt you to save any changes. To quit all windows without saving use :qa! (mnemonic: quit all in a possibly dangerous manner).