Use of ! in VIM

vivim

I have seen that sometimes :q works but sometimes we have to use :q!. This is the case for many commands. I was wondering what is the general use of ! in vim and when to use it. I tried to google this, but it seems the search is omitting the exclamation mark.

Best Answer

When you make no changes to the actual content of the file, you can simply quit with :q. However if you make edits, vim will not allow a simple quit because you may not want to abandon those changes (especially if you've been in vim for a long time editing and use :q by accident). The :q! in this case is a force the quit operation (override the warning). You can issue a forced quit to all opened windows (such as those opened with Ctrlwn) with :qa!.

You can write changes out and quit with :wq (or :x), and this sometimes will fail (the file has been opened as readonly (-R on the command line, or vim was invoked with the view command), in which case you can force the write operation with :wq!.

As an aside, you can also use ZQ to do the same operation as :q! and ZZ to do the same as :wq, which can be easier on the hands for typing :)

Vim also has a built-in help which you can access via :help; exiting has it's own quick topic page: :help Q_wq.