Basically I would like to know the difference between: :w
and :w!
or :wq
and :wq!

- 347,512
- 102
- 1,199
- 985

- 53,565
- 76
- 241
- 413
5 Answers
The ! qualifier tells Vim to force the operation. For example, if the file was read-only you would use :w! to write it anyway. If the file was modified and you wanted to quit without saving, you would use :q!. :wq! just means force write and quit in one command.

- 18,464
- 5
- 40
- 50
-
Yes I recently changed my mapping: Ctrl+S used to be :update and now is :w!, because I have a persistent ready-only file. Would I have problems in the future because of that change? – alexchenco Jun 22 '10 at 15:43
-
2@janoChen, yes, you'll get use to `Ctrl-S` and won't practice `:w!` enough :) – OscarRyz Jun 22 '10 at 16:11
-
@janoChen: Ctrl+S doesn't seem to have a default mapping so there should be no problem elsewhere. Your only issue might be accidentally saving over a read-only file that you didn't want to overwrite. – Amardeep AC9MF Jun 22 '10 at 16:44
-
@janoChen you may have problems with using `
` in terminal, using `:w!` instead of `:up` should not cause much problem because you hardly hit ` – ZyX Jun 22 '10 at 18:29` by accident.
In your examples the exclamation point means to force the action (e.g. :w!
will overwrite an existing file and :q!
will quit without saving).
That said, there are many other uses depending on the command, e.g.:
:set <option>!
toggles a boolean option, e.g.:set number!
!
followed by some shell command executes that command directly from the editor, e.g.:! ls /etc
:w !cmd
pipes the contents of the current buffer to the commandcmd
, e.g.:w !sudo tee %
(a.k.a. the write with sudo trick).

- 142,882
- 41
- 325
- 378
Besides the situations where the exclamation point forces things, like writes, it will turn a command into a toggle command. So if I do:
:set cursorline
the line my cursor is on will be highlighted. I can turn it off with:
:set nocursorline
Or I could do:
:set cursorline!
That command flips between the two settings, off and on.
I turn cursor line highlighting off and on frequently, and the toggle command lets me do it with a simple function key mapping. Without the toggle, I would need either two mappings: one to turn it on, and a second to turn it off. Or I would have to write a function to determine whether the cursorline setting was on or off, and then turn on the opposite setting.
This works with, as far as I know, all command line settings that have on and off settings, like hlsearch, paste, cursorcolumn, number, insearch, etc.
Note also that the exclamation point will toggle the no version of the command. For example, you could also toggle the cursor line setting with:
:set nocursorline!

- 1,661
- 1
- 12
- 6
It really depends on the command considered. Regarding the ones you have enumerated, it forces the command as others have already answered you.
However, there are other commands like :global
, :map
, :make
, :silent
, ..., where the bang (!
) has other effects. Read their documentation:
:help help
(and we can give the bang any meaning we want in the commands we define)

- 31,979
- 7
- 69
- 83
example | meaning |
---|---|
:wq! :q! |
do it anyway! |
:autocmd! {group} {event} {pat} cmd |
override specific autocommands (:help autocmd-remove* ) |
:function! |
override existing |
:com[mand][!] [{attr}...] {cmd} {repl} |
override existing |
:set number! |
(override 0 with 1, or 1 → 0) toggle an option |
:!ls |
shell command |
:com[mand][!] [{attr}...] {cmd} {repl}
Define a user command. The name of the command is
{cmd} and its replacement text is {repl}. The
command's attributes (see below) are {attr}. If the
command already exists, an error is reported, unless a
! is specified, in which case the command is
Command attributes
User-defined commands are treated by Vim just like any other Ex commands. They
can have arguments, or have a range specified. Arguments are subject to
completion as filenames, buffers, etc. Exactly how this works depends upon the
command's attributes, which are specified when the command is defined.
There are a number of attributes, split into four categories: argument
handling, completion behavior, range handling, and special cases. The
attributes are described below, by category.
......
Special cases
:command-bang :command-bar
:command-register :command-buffer
:command-keepscript
There are some special cases as well:
-bang The command can take a ! modifier (like :q or :w)
-bar The command can be followed by a "|" and another command.
A "|" inside the command argument is not allowed then.
Also checks for a " to start a comment.
shell
example | meaning |
---|---|
#! /bin/sh | shabang |
!! | last history |
!2 | second last history |
ptipython
example | meaning |
---|---|
:!ls |
shell command |

- 625
- 6
- 10