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.

  • 1
    ! means don't nag me with warnings; just do it. (it doesn't make that much sense in that example; as if the reason you got warnings was you don't have permissions to write the file, the ! has no effect anyway) – guiverc Nov 24 '19 at 6:58
  • 4
    Vi and Vim exists too! – D. Ben Knoble Nov 24 '19 at 23:03
  • Have you tried :help! – Stefano Borini Nov 25 '19 at 13:42

"!" 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).

  • Ah, ... I think I've got it. It forces the write-action, instead of displaying a warning. Saying that you are lacking the write-permission. You're example is great. Thanks a big bunch. – mizech Nov 24 '19 at 11:42
  • 3
    Another common case is invoking vim as view or vim -R, which opens the file in read-only mode even if you have permissions. That's useful to avoid accidentally saving, but if you change your mind later and want to save a change, :w! is easier than quitting and re-opening. – IMSoP Nov 24 '19 at 17:57
  • 3
    @IMSoP oh. So "read-only" isn't really "read-only"? – Eric Duminil Nov 25 '19 at 7:37
  • 2
    @EricDuminil Yeah... it's short-hand for "read only if I don't change my mind" :-) – TripeHound Nov 25 '19 at 9:56
  • 1
    When I was learning vi, I was told that "!" used in this way is pronounced "dammit". – Matt Malone Nov 25 '19 at 19:31

: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.

  • 1
    @filbranden Thank you for rewriting my answer so it's valid; though you certainly could have written it as your own answer instead of editing mine. I forgot to delete mine. – Ross Presser Dec 8 '19 at 0:03
  • 1
    It's a great answer, in fact it was my favorite here. Don't worry if you got it wrong at first... Editing until it looks good is totally fair. (Fun fact: one of my highest voted answers in one of the SE sites is one I got wrong at first and had to fix!) – filbranden Dec 8 '19 at 1:12

: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.

  • 1
    I've assumed it means “force write-and-quit”. I.e. it parses as :(wq)!. No? — The advantage of wq might be memorising only two letters, rather than separate w, q and x commands. – leftaroundabout Nov 25 '19 at 2:58
  • The advantage to vim is that it's very easy to chain together very simple commands into complex actions, and a lot of old timers (and people they've taught/influenced) still prefer wq. I however am lazy enough to just use x, but I recognize that I'm in the minority of vim users I know. – Jared Smith Nov 25 '19 at 11:33
  • :wq and :x have another slight difference in behavior, as :wq will write the file unconditionally (even if it was not modified), while :x will only write it if it has been modified inside Vim. It's a small distinction, but in some cases it might matter... – filbranden Dec 7 '19 at 6:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.