Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
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

1 Answer 1

up vote 13 down vote accepted

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 builtin help which you can access via :help, exiting has it's own quick topic page: :help Q_wq.

share|improve this answer
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.