wyc
wyc

Reputation: 55273

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

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

Upvotes: 67

Views: 47372

Answers (5)

Good Pen
Good Pen

Reputation: 801

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

ipython and ptipython

example meaning
:!ls shell command

Upvotes: 10

Eugene Yarmash
Eugene Yarmash

Reputation: 149796

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

Upvotes: 46

Andrew Langman
Andrew Langman

Reputation: 1771

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!

Upvotes: 40

Luc Hermitte
Luc Hermitte

Reputation: 32936

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)

Upvotes: 13

Amardeep AC9MF
Amardeep AC9MF

Reputation: 19044

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.

Upvotes: 67

Related Questions