Use of ! in VIM











up vote
6
down vote

favorite
4












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















up vote
6
down vote

favorite
4












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













up vote
6
down vote

favorite
4









up vote
6
down vote

favorite
4






4





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















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.







vim vi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 26 '13 at 17:22









Bernhard

7,60533966




7,60533966










asked Aug 26 '13 at 16:33









user2179293

5883813




5883813








  • 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














  • 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








6




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




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










2 Answers
2






active

oldest

votes

















up vote
20
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 a built-in 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




















up vote
2
down vote













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.






share|improve this answer

















  • 1




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











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f88247%2fuse-of-in-vim%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
20
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 a built-in 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

















up vote
20
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 a built-in 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















up vote
20
down vote



accepted







up vote
20
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 a built-in help which you can access via :help; exiting has it's own quick topic page: :help Q_wq.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday









Community

1




1










answered Aug 26 '13 at 16:42









Drav Sloan

9,57023138




9,57023138








  • 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




    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




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






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




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




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






: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














up vote
2
down vote













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.






share|improve this answer

















  • 1




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















up vote
2
down vote













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.






share|improve this answer

















  • 1




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













up vote
2
down vote










up vote
2
down vote









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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '17 at 8:47









Cédric Van Rompay

1213




1213








  • 1




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














  • 1




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








1




1




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




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


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f88247%2fuse-of-in-vim%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

サソリ

広島県道265号伴広島線

Accessing regular linux commands in Huawei's Dopra Linux