What does the '!' really do when it's added to an ex command (:wq! | :w! | :q! )?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












5















I understand that it means force to do something.



When doing $ vim -R file I enter in read-only mode, but this works as preventive mode only



-R Readonly mode. The 'readonly' option will be set for all the
files being edited. You can still edit the buffer, but will
be prevented from accidentally overwriting a file. If you
forgot that you are in View mode and did make some changes,
you can overwrite a file by adding an exclamation mark to
the Ex command, as in ":w!". The 'readonly' option can be
reset with ":set noro" (see the options chapter, |options|).
Subsequent edits will not be done in readonly mode. Calling
the executable "view" has the same effect as the -R argument.
The 'updatecount' option will be set to 10000, meaning that
the swap file will not be updated automatically very often.


But what I can't get yet is why it makes the instruction to skip permissions even if the file's owner is root user, besides it also change owner & group.










share|improve this question
























  • Where is the question? vim may do whatever it wishes inside itself, e.g. to imagine it edits a root-owned r-- file under an ordinary user. But it’s only an application, and when about to actually do something with files, the OS kernel will check permissions anyway.

    – Incnis Mrsi
    Sep 9 '15 at 18:51












  • Ok but then the -w- permission must not exist for vim...

    – tachomi
    Sep 9 '15 at 18:54






  • 2





    If you're interested in Vim, do checkout the sister site Vi and Vim.

    – muru
    Sep 9 '15 at 19:17
















5















I understand that it means force to do something.



When doing $ vim -R file I enter in read-only mode, but this works as preventive mode only



-R Readonly mode. The 'readonly' option will be set for all the
files being edited. You can still edit the buffer, but will
be prevented from accidentally overwriting a file. If you
forgot that you are in View mode and did make some changes,
you can overwrite a file by adding an exclamation mark to
the Ex command, as in ":w!". The 'readonly' option can be
reset with ":set noro" (see the options chapter, |options|).
Subsequent edits will not be done in readonly mode. Calling
the executable "view" has the same effect as the -R argument.
The 'updatecount' option will be set to 10000, meaning that
the swap file will not be updated automatically very often.


But what I can't get yet is why it makes the instruction to skip permissions even if the file's owner is root user, besides it also change owner & group.










share|improve this question
























  • Where is the question? vim may do whatever it wishes inside itself, e.g. to imagine it edits a root-owned r-- file under an ordinary user. But it’s only an application, and when about to actually do something with files, the OS kernel will check permissions anyway.

    – Incnis Mrsi
    Sep 9 '15 at 18:51












  • Ok but then the -w- permission must not exist for vim...

    – tachomi
    Sep 9 '15 at 18:54






  • 2





    If you're interested in Vim, do checkout the sister site Vi and Vim.

    – muru
    Sep 9 '15 at 19:17














5












5








5


1






I understand that it means force to do something.



When doing $ vim -R file I enter in read-only mode, but this works as preventive mode only



-R Readonly mode. The 'readonly' option will be set for all the
files being edited. You can still edit the buffer, but will
be prevented from accidentally overwriting a file. If you
forgot that you are in View mode and did make some changes,
you can overwrite a file by adding an exclamation mark to
the Ex command, as in ":w!". The 'readonly' option can be
reset with ":set noro" (see the options chapter, |options|).
Subsequent edits will not be done in readonly mode. Calling
the executable "view" has the same effect as the -R argument.
The 'updatecount' option will be set to 10000, meaning that
the swap file will not be updated automatically very often.


But what I can't get yet is why it makes the instruction to skip permissions even if the file's owner is root user, besides it also change owner & group.










share|improve this question
















I understand that it means force to do something.



When doing $ vim -R file I enter in read-only mode, but this works as preventive mode only



-R Readonly mode. The 'readonly' option will be set for all the
files being edited. You can still edit the buffer, but will
be prevented from accidentally overwriting a file. If you
forgot that you are in View mode and did make some changes,
you can overwrite a file by adding an exclamation mark to
the Ex command, as in ":w!". The 'readonly' option can be
reset with ":set noro" (see the options chapter, |options|).
Subsequent edits will not be done in readonly mode. Calling
the executable "view" has the same effect as the -R argument.
The 'updatecount' option will be set to 10000, meaning that
the swap file will not be updated automatically very often.


But what I can't get yet is why it makes the instruction to skip permissions even if the file's owner is root user, besides it also change owner & group.







vim






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 26 at 11:13









Jeff Schaller

43.8k1161141




43.8k1161141










asked Sep 9 '15 at 18:31









tachomitachomi

3,76231136




3,76231136












  • Where is the question? vim may do whatever it wishes inside itself, e.g. to imagine it edits a root-owned r-- file under an ordinary user. But it’s only an application, and when about to actually do something with files, the OS kernel will check permissions anyway.

    – Incnis Mrsi
    Sep 9 '15 at 18:51












  • Ok but then the -w- permission must not exist for vim...

    – tachomi
    Sep 9 '15 at 18:54






  • 2





    If you're interested in Vim, do checkout the sister site Vi and Vim.

    – muru
    Sep 9 '15 at 19:17


















  • Where is the question? vim may do whatever it wishes inside itself, e.g. to imagine it edits a root-owned r-- file under an ordinary user. But it’s only an application, and when about to actually do something with files, the OS kernel will check permissions anyway.

    – Incnis Mrsi
    Sep 9 '15 at 18:51












  • Ok but then the -w- permission must not exist for vim...

    – tachomi
    Sep 9 '15 at 18:54






  • 2





    If you're interested in Vim, do checkout the sister site Vi and Vim.

    – muru
    Sep 9 '15 at 19:17

















Where is the question? vim may do whatever it wishes inside itself, e.g. to imagine it edits a root-owned r-- file under an ordinary user. But it’s only an application, and when about to actually do something with files, the OS kernel will check permissions anyway.

– Incnis Mrsi
Sep 9 '15 at 18:51






Where is the question? vim may do whatever it wishes inside itself, e.g. to imagine it edits a root-owned r-- file under an ordinary user. But it’s only an application, and when about to actually do something with files, the OS kernel will check permissions anyway.

– Incnis Mrsi
Sep 9 '15 at 18:51














Ok but then the -w- permission must not exist for vim...

– tachomi
Sep 9 '15 at 18:54





Ok but then the -w- permission must not exist for vim...

– tachomi
Sep 9 '15 at 18:54




2




2





If you're interested in Vim, do checkout the sister site Vi and Vim.

– muru
Sep 9 '15 at 19:17






If you're interested in Vim, do checkout the sister site Vi and Vim.

– muru
Sep 9 '15 at 19:17











2 Answers
2






active

oldest

votes


















7














! generally means what you'd expect from "force", but what it means for specific command depends on the command. In the case of w!, if Vim cannot write to the file for some reason, it will try to delete and create a new one with the current buffer's contents.



Consider the following example (observe the inode numbers):



$ touch foo
$ chmod -w foo
$ stat foo
File: ‘foo’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 22h/34d Inode: 10396141 Links: 1
Access: (0444/-r--r--r--) Uid: ( 1000/ muru) Gid: ( 1000/ muru)
Access: 2015-09-10 00:24:28.259290486 +0530
Modify: 2015-09-10 00:24:28.259290486 +0530
Change: 2015-09-10 00:24:30.771263735 +0530
Birth: -
$ vim -c 'r!date' -c 'wq!' foo
$ stat foo
File: ‘foo’
Size: 30 Blocks: 8 IO Block: 4096 regular file
Device: 22h/34d Inode: 10396151 Links: 1
Access: (0444/-r--r--r--) Uid: ( 1000/ muru) Gid: ( 1000/ muru)
Access: 2015-09-10 00:24:37.727189657 +0530
Modify: 2015-09-10 00:24:37.731189614 +0530
Change: 2015-09-10 00:24:37.763189273 +0530
Birth: -
$ cat foo
Thu Sep 10 00:24:37 IST 2015


That's why the owner and group changes. Permissions are preserved - :h write-permissions:



 write-permissions
When writing a new file the permissions are read-write. For unix the mask is
0666 with additionally umask applied. When writing a file that was read Vim
will preserve the permissions, but clear the s-bit.


If you want to make Vim refuse writes, see :h write-readonly:



 write-readonly
When the 'cpoptions' option contains 'W', Vim will refuse to overwrite a
readonly file. When 'W' is not present, ":w!" will overwrite a readonly file,
if the system allows it (the directory must be writable).


Note that it says "the directory must be writable" - because without a writable directory, Vim can neither delete nor create a new file.






share|improve this answer
































    2














    As stated in vim(1), -R ensures that the file will not be accidentally overwritten when the user blindly says :w, but doesn’t disable writing with :w!.



    But it’s only an application, and when about to actually do something with files, the OS kernel will check permissions anyway. An experiment: I ran



    strace vim /etc/hostname 2>vim.out


    under a user.
    Even without explicit -R it started in readonly, because it looked at permissions.
    After changing the buffer



    W10: Warning: Changing a readonly file


    appeared.



    Now :w and I got



    E45: 'readonly' option is set (add ! to override)


    I applied the suggestion and



    "/etc/hostname" E212: Can't open file for writing


    Predictably, in vim.out we see:



    open("/etc/hostname", O_WRONLY|O_CREAT|O_TRUNC, 0644) = -1 EACCES (Permission denied)





    share|improve this answer
























      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',
      autoActivateHeartbeat: false,
      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%2f228624%2fwhat-does-the-really-do-when-its-added-to-an-ex-command-wq-w-q%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









      7














      ! generally means what you'd expect from "force", but what it means for specific command depends on the command. In the case of w!, if Vim cannot write to the file for some reason, it will try to delete and create a new one with the current buffer's contents.



      Consider the following example (observe the inode numbers):



      $ touch foo
      $ chmod -w foo
      $ stat foo
      File: ‘foo’
      Size: 0 Blocks: 0 IO Block: 4096 regular empty file
      Device: 22h/34d Inode: 10396141 Links: 1
      Access: (0444/-r--r--r--) Uid: ( 1000/ muru) Gid: ( 1000/ muru)
      Access: 2015-09-10 00:24:28.259290486 +0530
      Modify: 2015-09-10 00:24:28.259290486 +0530
      Change: 2015-09-10 00:24:30.771263735 +0530
      Birth: -
      $ vim -c 'r!date' -c 'wq!' foo
      $ stat foo
      File: ‘foo’
      Size: 30 Blocks: 8 IO Block: 4096 regular file
      Device: 22h/34d Inode: 10396151 Links: 1
      Access: (0444/-r--r--r--) Uid: ( 1000/ muru) Gid: ( 1000/ muru)
      Access: 2015-09-10 00:24:37.727189657 +0530
      Modify: 2015-09-10 00:24:37.731189614 +0530
      Change: 2015-09-10 00:24:37.763189273 +0530
      Birth: -
      $ cat foo
      Thu Sep 10 00:24:37 IST 2015


      That's why the owner and group changes. Permissions are preserved - :h write-permissions:



       write-permissions
      When writing a new file the permissions are read-write. For unix the mask is
      0666 with additionally umask applied. When writing a file that was read Vim
      will preserve the permissions, but clear the s-bit.


      If you want to make Vim refuse writes, see :h write-readonly:



       write-readonly
      When the 'cpoptions' option contains 'W', Vim will refuse to overwrite a
      readonly file. When 'W' is not present, ":w!" will overwrite a readonly file,
      if the system allows it (the directory must be writable).


      Note that it says "the directory must be writable" - because without a writable directory, Vim can neither delete nor create a new file.






      share|improve this answer





























        7














        ! generally means what you'd expect from "force", but what it means for specific command depends on the command. In the case of w!, if Vim cannot write to the file for some reason, it will try to delete and create a new one with the current buffer's contents.



        Consider the following example (observe the inode numbers):



        $ touch foo
        $ chmod -w foo
        $ stat foo
        File: ‘foo’
        Size: 0 Blocks: 0 IO Block: 4096 regular empty file
        Device: 22h/34d Inode: 10396141 Links: 1
        Access: (0444/-r--r--r--) Uid: ( 1000/ muru) Gid: ( 1000/ muru)
        Access: 2015-09-10 00:24:28.259290486 +0530
        Modify: 2015-09-10 00:24:28.259290486 +0530
        Change: 2015-09-10 00:24:30.771263735 +0530
        Birth: -
        $ vim -c 'r!date' -c 'wq!' foo
        $ stat foo
        File: ‘foo’
        Size: 30 Blocks: 8 IO Block: 4096 regular file
        Device: 22h/34d Inode: 10396151 Links: 1
        Access: (0444/-r--r--r--) Uid: ( 1000/ muru) Gid: ( 1000/ muru)
        Access: 2015-09-10 00:24:37.727189657 +0530
        Modify: 2015-09-10 00:24:37.731189614 +0530
        Change: 2015-09-10 00:24:37.763189273 +0530
        Birth: -
        $ cat foo
        Thu Sep 10 00:24:37 IST 2015


        That's why the owner and group changes. Permissions are preserved - :h write-permissions:



         write-permissions
        When writing a new file the permissions are read-write. For unix the mask is
        0666 with additionally umask applied. When writing a file that was read Vim
        will preserve the permissions, but clear the s-bit.


        If you want to make Vim refuse writes, see :h write-readonly:



         write-readonly
        When the 'cpoptions' option contains 'W', Vim will refuse to overwrite a
        readonly file. When 'W' is not present, ":w!" will overwrite a readonly file,
        if the system allows it (the directory must be writable).


        Note that it says "the directory must be writable" - because without a writable directory, Vim can neither delete nor create a new file.






        share|improve this answer



























          7












          7








          7







          ! generally means what you'd expect from "force", but what it means for specific command depends on the command. In the case of w!, if Vim cannot write to the file for some reason, it will try to delete and create a new one with the current buffer's contents.



          Consider the following example (observe the inode numbers):



          $ touch foo
          $ chmod -w foo
          $ stat foo
          File: ‘foo’
          Size: 0 Blocks: 0 IO Block: 4096 regular empty file
          Device: 22h/34d Inode: 10396141 Links: 1
          Access: (0444/-r--r--r--) Uid: ( 1000/ muru) Gid: ( 1000/ muru)
          Access: 2015-09-10 00:24:28.259290486 +0530
          Modify: 2015-09-10 00:24:28.259290486 +0530
          Change: 2015-09-10 00:24:30.771263735 +0530
          Birth: -
          $ vim -c 'r!date' -c 'wq!' foo
          $ stat foo
          File: ‘foo’
          Size: 30 Blocks: 8 IO Block: 4096 regular file
          Device: 22h/34d Inode: 10396151 Links: 1
          Access: (0444/-r--r--r--) Uid: ( 1000/ muru) Gid: ( 1000/ muru)
          Access: 2015-09-10 00:24:37.727189657 +0530
          Modify: 2015-09-10 00:24:37.731189614 +0530
          Change: 2015-09-10 00:24:37.763189273 +0530
          Birth: -
          $ cat foo
          Thu Sep 10 00:24:37 IST 2015


          That's why the owner and group changes. Permissions are preserved - :h write-permissions:



           write-permissions
          When writing a new file the permissions are read-write. For unix the mask is
          0666 with additionally umask applied. When writing a file that was read Vim
          will preserve the permissions, but clear the s-bit.


          If you want to make Vim refuse writes, see :h write-readonly:



           write-readonly
          When the 'cpoptions' option contains 'W', Vim will refuse to overwrite a
          readonly file. When 'W' is not present, ":w!" will overwrite a readonly file,
          if the system allows it (the directory must be writable).


          Note that it says "the directory must be writable" - because without a writable directory, Vim can neither delete nor create a new file.






          share|improve this answer















          ! generally means what you'd expect from "force", but what it means for specific command depends on the command. In the case of w!, if Vim cannot write to the file for some reason, it will try to delete and create a new one with the current buffer's contents.



          Consider the following example (observe the inode numbers):



          $ touch foo
          $ chmod -w foo
          $ stat foo
          File: ‘foo’
          Size: 0 Blocks: 0 IO Block: 4096 regular empty file
          Device: 22h/34d Inode: 10396141 Links: 1
          Access: (0444/-r--r--r--) Uid: ( 1000/ muru) Gid: ( 1000/ muru)
          Access: 2015-09-10 00:24:28.259290486 +0530
          Modify: 2015-09-10 00:24:28.259290486 +0530
          Change: 2015-09-10 00:24:30.771263735 +0530
          Birth: -
          $ vim -c 'r!date' -c 'wq!' foo
          $ stat foo
          File: ‘foo’
          Size: 30 Blocks: 8 IO Block: 4096 regular file
          Device: 22h/34d Inode: 10396151 Links: 1
          Access: (0444/-r--r--r--) Uid: ( 1000/ muru) Gid: ( 1000/ muru)
          Access: 2015-09-10 00:24:37.727189657 +0530
          Modify: 2015-09-10 00:24:37.731189614 +0530
          Change: 2015-09-10 00:24:37.763189273 +0530
          Birth: -
          $ cat foo
          Thu Sep 10 00:24:37 IST 2015


          That's why the owner and group changes. Permissions are preserved - :h write-permissions:



           write-permissions
          When writing a new file the permissions are read-write. For unix the mask is
          0666 with additionally umask applied. When writing a file that was read Vim
          will preserve the permissions, but clear the s-bit.


          If you want to make Vim refuse writes, see :h write-readonly:



           write-readonly
          When the 'cpoptions' option contains 'W', Vim will refuse to overwrite a
          readonly file. When 'W' is not present, ":w!" will overwrite a readonly file,
          if the system allows it (the directory must be writable).


          Note that it says "the directory must be writable" - because without a writable directory, Vim can neither delete nor create a new file.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 9 '15 at 19:11

























          answered Sep 9 '15 at 19:01









          murumuru

          36.5k589163




          36.5k589163























              2














              As stated in vim(1), -R ensures that the file will not be accidentally overwritten when the user blindly says :w, but doesn’t disable writing with :w!.



              But it’s only an application, and when about to actually do something with files, the OS kernel will check permissions anyway. An experiment: I ran



              strace vim /etc/hostname 2>vim.out


              under a user.
              Even without explicit -R it started in readonly, because it looked at permissions.
              After changing the buffer



              W10: Warning: Changing a readonly file


              appeared.



              Now :w and I got



              E45: 'readonly' option is set (add ! to override)


              I applied the suggestion and



              "/etc/hostname" E212: Can't open file for writing


              Predictably, in vim.out we see:



              open("/etc/hostname", O_WRONLY|O_CREAT|O_TRUNC, 0644) = -1 EACCES (Permission denied)





              share|improve this answer





























                2














                As stated in vim(1), -R ensures that the file will not be accidentally overwritten when the user blindly says :w, but doesn’t disable writing with :w!.



                But it’s only an application, and when about to actually do something with files, the OS kernel will check permissions anyway. An experiment: I ran



                strace vim /etc/hostname 2>vim.out


                under a user.
                Even without explicit -R it started in readonly, because it looked at permissions.
                After changing the buffer



                W10: Warning: Changing a readonly file


                appeared.



                Now :w and I got



                E45: 'readonly' option is set (add ! to override)


                I applied the suggestion and



                "/etc/hostname" E212: Can't open file for writing


                Predictably, in vim.out we see:



                open("/etc/hostname", O_WRONLY|O_CREAT|O_TRUNC, 0644) = -1 EACCES (Permission denied)





                share|improve this answer



























                  2












                  2








                  2







                  As stated in vim(1), -R ensures that the file will not be accidentally overwritten when the user blindly says :w, but doesn’t disable writing with :w!.



                  But it’s only an application, and when about to actually do something with files, the OS kernel will check permissions anyway. An experiment: I ran



                  strace vim /etc/hostname 2>vim.out


                  under a user.
                  Even without explicit -R it started in readonly, because it looked at permissions.
                  After changing the buffer



                  W10: Warning: Changing a readonly file


                  appeared.



                  Now :w and I got



                  E45: 'readonly' option is set (add ! to override)


                  I applied the suggestion and



                  "/etc/hostname" E212: Can't open file for writing


                  Predictably, in vim.out we see:



                  open("/etc/hostname", O_WRONLY|O_CREAT|O_TRUNC, 0644) = -1 EACCES (Permission denied)





                  share|improve this answer















                  As stated in vim(1), -R ensures that the file will not be accidentally overwritten when the user blindly says :w, but doesn’t disable writing with :w!.



                  But it’s only an application, and when about to actually do something with files, the OS kernel will check permissions anyway. An experiment: I ran



                  strace vim /etc/hostname 2>vim.out


                  under a user.
                  Even without explicit -R it started in readonly, because it looked at permissions.
                  After changing the buffer



                  W10: Warning: Changing a readonly file


                  appeared.



                  Now :w and I got



                  E45: 'readonly' option is set (add ! to override)


                  I applied the suggestion and



                  "/etc/hostname" E212: Can't open file for writing


                  Predictably, in vim.out we see:



                  open("/etc/hostname", O_WRONLY|O_CREAT|O_TRUNC, 0644) = -1 EACCES (Permission denied)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 9 '15 at 19:10









                  muru

                  36.5k589163




                  36.5k589163










                  answered Sep 9 '15 at 19:08









                  Incnis MrsiIncnis Mrsi

                  1,3861023




                  1,3861023



























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f228624%2fwhat-does-the-really-do-when-its-added-to-an-ex-command-wq-w-q%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

                      Peggy Mitchell

                      The Forum (Inglewood, California)

                      Palaiologos