Basically I would like to know the difference between: :w
and :w!
or :wq
and :wq!
Overview. If we work a lot with the interactive Linux command line, chances are we might have come across the exclamation symbol. On most shells, this symbol is used to rerun previously executed commands through history expansion, thereby, increasing our productivity.
The double factorial, symbolized by two exclamation marks (!!), is a quantity defined for all integer s greater than or equal to -1. For an even integer n , the double factorial is the product of all even integers less than or equal to n but greater than or equal to 2.
The exclamation mark (!), known informally as a bang or a shriek, is used at the end of a sentence or a short phrase which expresses very strong feeling. Here are some examples: What a lovely view you have here! That's fantastic!
at the start of a pattern, by either using a backslash ( \! ), or otherwise making it not the first character of the regex ( /[!] , for example).
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.
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 command cmd
, e.g. :w !sudo tee %
(a.k.a. the write with sudo trick).
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With