Vim: Exit (:wq) with trailing exclamation mark ! What's the purpose?

If one leaves an edited file with :q!, then it discards the updates. If one leaves with with :wq, then it writes the updates and quits Vim.

But what's the purpose of :wq! (with a trailing exclamation mark)?

I have found no good explanation about that.

3

3 Answers

"!" means don't nag me with warnings; just do it.

If you try and vim /etc/hosts, and make changes and try and save with :wq! - the "!" is moot. That is a real error that can't be forced thus use of "!" won't work.

A useful example..

touch ~/example
chmod -w ~/example
vim ~/example

If you open a file where you have READ access only but have taken away your WRITE access before hand, that warning can be overridden by the "!" (unlike permissions error in /etc/), thus a ":wq!" in this case is handy (quicker than jumping to shell to fix).

5

:wq! means "write this buffer then close it, no questions asked." If you have any other buffers open, they stay open and vim doesn't exit.

2

:x! does the same thing and I have no idea where :wq! came from in modern documentation since it was obsolete 4 decades ago.

The problem with wq! is does that mean write then force quit or force write and then quit? With x! it is clear.

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like