6

I have seen that sometimes :q works but sometimes we have to use :q!. This is the case for many commands. I was wondering what is the general use of ! in vim and when to use it. I tried to google this, but it seems the search is omitting the exclamation mark.

  • 6
    If you have vim installed on your computer, you should also have the interactive vim tutor. From the command-line (NOT from within vim) use vimtutor. It takes about half an hour and is the best way to learn the basics of vi/vim. – evilsoup Aug 26 '13 at 17:13
22

When you make no changes to the actual content of the file, you can simply quit with :q. However if you make edits, vim will not allow a simple quit because you may not want to abandon those changes (especially if you've been in vim for a long time editing and use :q by accident). The :q! in this case is a force the quit operation (override the warning). You can issue a forced quit to all opened windows (such as those opened with Ctrlwn) with :qa!.

You can write changes out and quit with :wq (or :x), and this sometimes will fail (the file has been opened as readonly (-R on the command line, or vim was invoked with the view command), in which case you can force the write operation with :wq!.

As an aside, you can also use ZQ to do the same operation as :q! and ZZ to do the same as :wq, which can be easier on the hands for typing :)

Vim also has a built-in help which you can access via :help; exiting has it's own quick topic page: :help Q_wq.

  • 2
    On a different note, you can also use ! to run shell commands as in :!ls -l – Joseph R. Aug 26 '13 at 16:48
  • 1
    how does ! in case of ':r! command' is it for forcing the output of command onto file – user2179293 Aug 26 '13 at 16:49
  • :r! command is very similar to !!command, the former will insert the command output on the following line following the cursor, the latter will replace the contents of the existing line. – Drav Sloan Aug 26 '13 at 16:54
2

There is no "general use" of !, it's just a modifier. Each command gets to chose what an extra ! will do to it.

In many cases commands are programmed so that ! forces them. Examples include q that you mention, but also save: save! will overwrite the file if it already exists, while save would return an error and suggest to use save! if you are sure of what you're doing.

Finally ! used on its own is used for shell commands, for instance :!ls or :!mkdir foo.

In the case of read (from your comment), the command read! <something> is interpreted by Vim as "execute <something> and read (meaning, in Vim, write to buffer) its output". So here it is as if the ! behaves not as a modifier of the read command but as the stand-alone command !. It could be that the read command does not declare a variant with ! so Vim just interprets the rest independently (so it reads a "! used alone" after read), or that read declares a variant with ! that has this particular behavior ("execute the rest as shell") that happens to mimic what the "! used alone" does.

I guess the take-away is: there is not rule, each command defines what ! will do, and while there is an attempt to have some sort of consistency in the effect of modifiers, not having super-strict rules (like "! will always be for forcing") allow to have more useful behaviors with less keystrokes, which is a plus too.

  • 1
    ... also the annoying difference between :w!cat and :w !cat – muru Nov 15 '17 at 8:52

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.