What's the use of the exclamation mark ('!') in Vim's command line after certain commands like ":w!"?

Vim

Vim Problem Overview


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

Vim Solutions


Solution 1 - Vim

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.

Solution 2 - Vim

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

Solution 3 - Vim

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!

Solution 4 - Vim

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)

Solution 5 - Vim

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalexchencoView Question on Stackoverflow
Solution 1 - VimAmardeep AC9MFView Answer on Stackoverflow
Solution 2 - VimEugene YarmashView Answer on Stackoverflow
Solution 3 - VimAndrew LangmanView Answer on Stackoverflow
Solution 4 - VimLuc HermitteView Answer on Stackoverflow
Solution 5 - VimGood PenView Answer on Stackoverflow