How vim overwrites readonly mode?Vim colorize edit modeDifferent vim colorscheme depending on modeSwitching...

How much light is too much?

What is a good reason for every spaceship to carry gun on board?

How can I give a Ranger advantage on a check due to Favored Enemy without spoiling the story for the player?

Is it really OK to use "because of"?

Renting a 2CV in France

Why do neural networks need so many examples to perform?

Democratic Socialism vs Social Democracy

Illustrator to chemdraw

Equivalent of "illegal" for violating civil law

Other than edits for international editions, did Harry Potter and the Philosopher's Stone receive errata?

If I tried and failed to start my own business, how do I apply for a job without job experience?

Remove isolated elements of a vector

Why didn't Tom Riddle take the presence of Fawkes and the Sorting Hat as more of a threat?

How to fly a direct entry holding pattern when approaching from an awkward angle?

Coworker asking me to not bring cakes due to self control issue. What should I do?

Reading Mishnayos without understanding

Prevent Nautilus / Nemo from creating .Trash-1000 folder in mounted devices

Why is Shelob considered evil?

"I showed the monkey himself in the mirror". Why is this sentence grammatical?

Case protection with emphasis in biblatex

Buying a "Used" Router

What are some ways of extending a description of a scenery?

Does it take energy to move something in a circle?

Is there a file that always exists and a 'normal' user can't lstat it?



How vim overwrites readonly mode?


Vim colorize edit modeDifferent vim colorscheme depending on modeSwitching to edit mode in VIM rc?Can you further integrate and present sequential moreutils vipe command output in vim using successive splits etc?Glitch with auto chmod line in .vimrcCan vim edit a remote file as root?`vim` brings me to lockout [Unity bug? Solved…ish]Any way to recall the file of previous cmd?Why is vim frozen?Why updates overwrites Vim that built from source?













1















Quite often we see that the file we are trying to save in vim after edit is reported to be read-only. The way around this is to add !wq, i am trying to figure out what internally goes that allows the vim program to gain enough permission to write the read-only file ?



Is there a internal flag which is switched or the vim temporarily gains the privileges for some time ?










share|improve this question



























    1















    Quite often we see that the file we are trying to save in vim after edit is reported to be read-only. The way around this is to add !wq, i am trying to figure out what internally goes that allows the vim program to gain enough permission to write the read-only file ?



    Is there a internal flag which is switched or the vim temporarily gains the privileges for some time ?










    share|improve this question

























      1












      1








      1








      Quite often we see that the file we are trying to save in vim after edit is reported to be read-only. The way around this is to add !wq, i am trying to figure out what internally goes that allows the vim program to gain enough permission to write the read-only file ?



      Is there a internal flag which is switched or the vim temporarily gains the privileges for some time ?










      share|improve this question














      Quite often we see that the file we are trying to save in vim after edit is reported to be read-only. The way around this is to add !wq, i am trying to figure out what internally goes that allows the vim program to gain enough permission to write the read-only file ?



      Is there a internal flag which is switched or the vim temporarily gains the privileges for some time ?







      shell vim






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 hours ago









      AtulAtul

      3662519




      3662519






















          3 Answers
          3






          active

          oldest

          votes


















          1














          Vim cannot gain additional permissions. The :w! just overrides the internal 'readonly' option, which may have been set because:




          • you've opened the file via the -R command-line option or with :view instead of :edit, or :setlocal readonly

          • Vim recognizes that the file currently doesn't have write permissions


          For the latter case, a file write may still be possible because Vim (by default) creates a new file and then replaces the original file with it. That still depends on the permissions being set in a way to allow this.





          To really gain write permissions where the user that opened Vim doesn't have any, the :w !sudo tee >/dev/null file trick has to be used, either directly, or through a plugin like SudoEdit.






          share|improve this answer































            1














            You can't ever write to a file that you don't have write permissions on. However you can delete that file if you have write permissions on the directory.



            The trick that VIM is using is to delete the file and write a new one.



            It's possible to show this is the method VIM uses without reading source code by checking the inode number before and after:



            $ touch foo
            $ chmod u-w foo
            $ ls -li foo
            60818465 -r--r----- 1 philip philip 0 Feb 25 10:24 foo
            $ vi foo
            $ # edit the file and save with :w!
            $ ls -li foo
            60818467 -r--r----- 1 philip philip 8 Feb 25 10:25 foo


            Note that the inode number changed showing that the new file is NOT the same file as the one you edited.





            FYI My current config is really short:



            runtime! debian.vim
            if has("syntax")
            syntax on
            endif
            set tabstop=4
            set autoindent


            And debian packages installed are:



            vim                               2:8.1.0875-1
            vim-common 2:8.1.0875-1
            vim-runtime 2:8.1.0875-1
            vim-tiny 2:8.1.0875-1





            share|improve this answer


























            • Changing permissions on the file is exactly what Vim does, at least on OpenBSD.

              – Kusalananda
              2 hours ago











            • @Kusalananda as per the experement above, not in Debian with Vim 2:8.1.0875-1. The file is clearly being completely replaced.

              – Philip Couling
              2 hours ago













            • This is not the method that VIM uses by default. The doco (:help :write) states that it temporarily changes permissions, and a quick truss confirms that the doco is right. You have non-default backup options set.

              – JdeBP
              2 hours ago











            • The phrase "by default" means "what happens if not otherwise configired" no? And :set backupcopy? results in backupcopy=auto. I've checked my config. There's nothing in there to change this behaviour (I think?)

              – Philip Couling
              2 hours ago













            • when vim is in compatible mode, :w! will only override the read-only mode of the buffer, but will not try to change the file perms back and forth or do other amateur hour stuff. This is the only right behavior -- it would be nice if it was clearly mentioned which one of the options turned on or off by :set compatible is responsible for this.

              – mosvy
              1 hour ago



















            1














            When you do w! in Vim, what actually happens depends on who owns the file.




            • If you (the current user) are the owner of the file, Vim will change the permissions to be writable before rewriting the file. It then removes the write permissions to restore the permission bits to what it was from the start.


            • If you are not the owner of the file, but if you have write permissions in the current directory, Vim will delete the original file and write the document to a new file with the same name. The new file will then be assigned the same permissions as the original file, but will be owned by you.





            This is the first case (output from ktrace+kdump on OpenBSD):



            13228 vim      CALL  chmod(0x19b1d94b4b10,0100644<S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH|S_IFREG>)
            13228 vim NAMI "file"
            13228 vim RET chmod 0
            13228 vim CALL lseek(3,0x1000,SEEK_SET)
            13228 vim RET lseek 4096/0x1000
            13228 vim CALL write(3,0x19b1e0aa9000,0x1000)


            This changes permissions on the file so that it's writable (the S_IWUSR flag used with chmod()) and writes the buffer to it.



            It then sets the original permissions:



            13228 vim      CALL  fchmod(4,0100444<S_IRUSR|S_IRGRP|S_IROTH|S_IFREG>)
            13228 vim RET fchmod 0
            13228 vim CALL close(4)
            13228 vim RET close 0


            For the other case:



            It first unlinks (deletes) the file and then recreates it (before writing to the file and changing permissions later):



            44487 vim      CALL  unlink(0x79fdbc1f000)
            44487 vim NAMI "file"
            44487 vim RET unlink 0
            44487 vim CALL open(0x79fdbc1f000,0x201<O_WRONLY|O_CREAT>,0644<S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH>)
            44487 vim NAMI "file"
            44487 vim RET open 4





            share|improve this answer


























            • It's really odd that you would get different results (even on a different OS) for the same software. Could you confirm the version you are using and the value of the two relavent config options you mention (backup and backupcopy)

              – Philip Couling
              1 hour ago











            • @PhilipCouling Vim 8.1.800. Both backup and backupcopy are unset (this is without a private .vimrc file).

              – Kusalananda
              1 hour ago











            • Likewise neither of mine are set in config and 8.1.800 is very close to 8.1.875, could you also confirm the result of set backup? and set backupcopy? It's plausible the good people at Debian compiled in something different.

              – Philip Couling
              1 hour ago








            • 1





              I did a truss on FreeBSD of both VIM and NeoVIM. I even checked nvi for good measure. I duplicated Kusalananda's results with all three.

              – JdeBP
              1 hour ago






            • 1





              @JdeBP Yes I'm rapidly becoming suspicious I'm seeing something unusual or Debian specific.

              – Philip Couling
              1 hour ago











            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%2f502826%2fhow-vim-overwrites-readonly-mode%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Vim cannot gain additional permissions. The :w! just overrides the internal 'readonly' option, which may have been set because:




            • you've opened the file via the -R command-line option or with :view instead of :edit, or :setlocal readonly

            • Vim recognizes that the file currently doesn't have write permissions


            For the latter case, a file write may still be possible because Vim (by default) creates a new file and then replaces the original file with it. That still depends on the permissions being set in a way to allow this.





            To really gain write permissions where the user that opened Vim doesn't have any, the :w !sudo tee >/dev/null file trick has to be used, either directly, or through a plugin like SudoEdit.






            share|improve this answer




























              1














              Vim cannot gain additional permissions. The :w! just overrides the internal 'readonly' option, which may have been set because:




              • you've opened the file via the -R command-line option or with :view instead of :edit, or :setlocal readonly

              • Vim recognizes that the file currently doesn't have write permissions


              For the latter case, a file write may still be possible because Vim (by default) creates a new file and then replaces the original file with it. That still depends on the permissions being set in a way to allow this.





              To really gain write permissions where the user that opened Vim doesn't have any, the :w !sudo tee >/dev/null file trick has to be used, either directly, or through a plugin like SudoEdit.






              share|improve this answer


























                1












                1








                1







                Vim cannot gain additional permissions. The :w! just overrides the internal 'readonly' option, which may have been set because:




                • you've opened the file via the -R command-line option or with :view instead of :edit, or :setlocal readonly

                • Vim recognizes that the file currently doesn't have write permissions


                For the latter case, a file write may still be possible because Vim (by default) creates a new file and then replaces the original file with it. That still depends on the permissions being set in a way to allow this.





                To really gain write permissions where the user that opened Vim doesn't have any, the :w !sudo tee >/dev/null file trick has to be used, either directly, or through a plugin like SudoEdit.






                share|improve this answer













                Vim cannot gain additional permissions. The :w! just overrides the internal 'readonly' option, which may have been set because:




                • you've opened the file via the -R command-line option or with :view instead of :edit, or :setlocal readonly

                • Vim recognizes that the file currently doesn't have write permissions


                For the latter case, a file write may still be possible because Vim (by default) creates a new file and then replaces the original file with it. That still depends on the permissions being set in a way to allow this.





                To really gain write permissions where the user that opened Vim doesn't have any, the :w !sudo tee >/dev/null file trick has to be used, either directly, or through a plugin like SudoEdit.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 hours ago









                Ingo KarkatIngo Karkat

                8,77911932




                8,77911932

























                    1














                    You can't ever write to a file that you don't have write permissions on. However you can delete that file if you have write permissions on the directory.



                    The trick that VIM is using is to delete the file and write a new one.



                    It's possible to show this is the method VIM uses without reading source code by checking the inode number before and after:



                    $ touch foo
                    $ chmod u-w foo
                    $ ls -li foo
                    60818465 -r--r----- 1 philip philip 0 Feb 25 10:24 foo
                    $ vi foo
                    $ # edit the file and save with :w!
                    $ ls -li foo
                    60818467 -r--r----- 1 philip philip 8 Feb 25 10:25 foo


                    Note that the inode number changed showing that the new file is NOT the same file as the one you edited.





                    FYI My current config is really short:



                    runtime! debian.vim
                    if has("syntax")
                    syntax on
                    endif
                    set tabstop=4
                    set autoindent


                    And debian packages installed are:



                    vim                               2:8.1.0875-1
                    vim-common 2:8.1.0875-1
                    vim-runtime 2:8.1.0875-1
                    vim-tiny 2:8.1.0875-1





                    share|improve this answer


























                    • Changing permissions on the file is exactly what Vim does, at least on OpenBSD.

                      – Kusalananda
                      2 hours ago











                    • @Kusalananda as per the experement above, not in Debian with Vim 2:8.1.0875-1. The file is clearly being completely replaced.

                      – Philip Couling
                      2 hours ago













                    • This is not the method that VIM uses by default. The doco (:help :write) states that it temporarily changes permissions, and a quick truss confirms that the doco is right. You have non-default backup options set.

                      – JdeBP
                      2 hours ago











                    • The phrase "by default" means "what happens if not otherwise configired" no? And :set backupcopy? results in backupcopy=auto. I've checked my config. There's nothing in there to change this behaviour (I think?)

                      – Philip Couling
                      2 hours ago













                    • when vim is in compatible mode, :w! will only override the read-only mode of the buffer, but will not try to change the file perms back and forth or do other amateur hour stuff. This is the only right behavior -- it would be nice if it was clearly mentioned which one of the options turned on or off by :set compatible is responsible for this.

                      – mosvy
                      1 hour ago
















                    1














                    You can't ever write to a file that you don't have write permissions on. However you can delete that file if you have write permissions on the directory.



                    The trick that VIM is using is to delete the file and write a new one.



                    It's possible to show this is the method VIM uses without reading source code by checking the inode number before and after:



                    $ touch foo
                    $ chmod u-w foo
                    $ ls -li foo
                    60818465 -r--r----- 1 philip philip 0 Feb 25 10:24 foo
                    $ vi foo
                    $ # edit the file and save with :w!
                    $ ls -li foo
                    60818467 -r--r----- 1 philip philip 8 Feb 25 10:25 foo


                    Note that the inode number changed showing that the new file is NOT the same file as the one you edited.





                    FYI My current config is really short:



                    runtime! debian.vim
                    if has("syntax")
                    syntax on
                    endif
                    set tabstop=4
                    set autoindent


                    And debian packages installed are:



                    vim                               2:8.1.0875-1
                    vim-common 2:8.1.0875-1
                    vim-runtime 2:8.1.0875-1
                    vim-tiny 2:8.1.0875-1





                    share|improve this answer


























                    • Changing permissions on the file is exactly what Vim does, at least on OpenBSD.

                      – Kusalananda
                      2 hours ago











                    • @Kusalananda as per the experement above, not in Debian with Vim 2:8.1.0875-1. The file is clearly being completely replaced.

                      – Philip Couling
                      2 hours ago













                    • This is not the method that VIM uses by default. The doco (:help :write) states that it temporarily changes permissions, and a quick truss confirms that the doco is right. You have non-default backup options set.

                      – JdeBP
                      2 hours ago











                    • The phrase "by default" means "what happens if not otherwise configired" no? And :set backupcopy? results in backupcopy=auto. I've checked my config. There's nothing in there to change this behaviour (I think?)

                      – Philip Couling
                      2 hours ago













                    • when vim is in compatible mode, :w! will only override the read-only mode of the buffer, but will not try to change the file perms back and forth or do other amateur hour stuff. This is the only right behavior -- it would be nice if it was clearly mentioned which one of the options turned on or off by :set compatible is responsible for this.

                      – mosvy
                      1 hour ago














                    1












                    1








                    1







                    You can't ever write to a file that you don't have write permissions on. However you can delete that file if you have write permissions on the directory.



                    The trick that VIM is using is to delete the file and write a new one.



                    It's possible to show this is the method VIM uses without reading source code by checking the inode number before and after:



                    $ touch foo
                    $ chmod u-w foo
                    $ ls -li foo
                    60818465 -r--r----- 1 philip philip 0 Feb 25 10:24 foo
                    $ vi foo
                    $ # edit the file and save with :w!
                    $ ls -li foo
                    60818467 -r--r----- 1 philip philip 8 Feb 25 10:25 foo


                    Note that the inode number changed showing that the new file is NOT the same file as the one you edited.





                    FYI My current config is really short:



                    runtime! debian.vim
                    if has("syntax")
                    syntax on
                    endif
                    set tabstop=4
                    set autoindent


                    And debian packages installed are:



                    vim                               2:8.1.0875-1
                    vim-common 2:8.1.0875-1
                    vim-runtime 2:8.1.0875-1
                    vim-tiny 2:8.1.0875-1





                    share|improve this answer















                    You can't ever write to a file that you don't have write permissions on. However you can delete that file if you have write permissions on the directory.



                    The trick that VIM is using is to delete the file and write a new one.



                    It's possible to show this is the method VIM uses without reading source code by checking the inode number before and after:



                    $ touch foo
                    $ chmod u-w foo
                    $ ls -li foo
                    60818465 -r--r----- 1 philip philip 0 Feb 25 10:24 foo
                    $ vi foo
                    $ # edit the file and save with :w!
                    $ ls -li foo
                    60818467 -r--r----- 1 philip philip 8 Feb 25 10:25 foo


                    Note that the inode number changed showing that the new file is NOT the same file as the one you edited.





                    FYI My current config is really short:



                    runtime! debian.vim
                    if has("syntax")
                    syntax on
                    endif
                    set tabstop=4
                    set autoindent


                    And debian packages installed are:



                    vim                               2:8.1.0875-1
                    vim-common 2:8.1.0875-1
                    vim-runtime 2:8.1.0875-1
                    vim-tiny 2:8.1.0875-1






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 1 hour ago

























                    answered 2 hours ago









                    Philip CoulingPhilip Couling

                    1,201917




                    1,201917













                    • Changing permissions on the file is exactly what Vim does, at least on OpenBSD.

                      – Kusalananda
                      2 hours ago











                    • @Kusalananda as per the experement above, not in Debian with Vim 2:8.1.0875-1. The file is clearly being completely replaced.

                      – Philip Couling
                      2 hours ago













                    • This is not the method that VIM uses by default. The doco (:help :write) states that it temporarily changes permissions, and a quick truss confirms that the doco is right. You have non-default backup options set.

                      – JdeBP
                      2 hours ago











                    • The phrase "by default" means "what happens if not otherwise configired" no? And :set backupcopy? results in backupcopy=auto. I've checked my config. There's nothing in there to change this behaviour (I think?)

                      – Philip Couling
                      2 hours ago













                    • when vim is in compatible mode, :w! will only override the read-only mode of the buffer, but will not try to change the file perms back and forth or do other amateur hour stuff. This is the only right behavior -- it would be nice if it was clearly mentioned which one of the options turned on or off by :set compatible is responsible for this.

                      – mosvy
                      1 hour ago



















                    • Changing permissions on the file is exactly what Vim does, at least on OpenBSD.

                      – Kusalananda
                      2 hours ago











                    • @Kusalananda as per the experement above, not in Debian with Vim 2:8.1.0875-1. The file is clearly being completely replaced.

                      – Philip Couling
                      2 hours ago













                    • This is not the method that VIM uses by default. The doco (:help :write) states that it temporarily changes permissions, and a quick truss confirms that the doco is right. You have non-default backup options set.

                      – JdeBP
                      2 hours ago











                    • The phrase "by default" means "what happens if not otherwise configired" no? And :set backupcopy? results in backupcopy=auto. I've checked my config. There's nothing in there to change this behaviour (I think?)

                      – Philip Couling
                      2 hours ago













                    • when vim is in compatible mode, :w! will only override the read-only mode of the buffer, but will not try to change the file perms back and forth or do other amateur hour stuff. This is the only right behavior -- it would be nice if it was clearly mentioned which one of the options turned on or off by :set compatible is responsible for this.

                      – mosvy
                      1 hour ago

















                    Changing permissions on the file is exactly what Vim does, at least on OpenBSD.

                    – Kusalananda
                    2 hours ago





                    Changing permissions on the file is exactly what Vim does, at least on OpenBSD.

                    – Kusalananda
                    2 hours ago













                    @Kusalananda as per the experement above, not in Debian with Vim 2:8.1.0875-1. The file is clearly being completely replaced.

                    – Philip Couling
                    2 hours ago







                    @Kusalananda as per the experement above, not in Debian with Vim 2:8.1.0875-1. The file is clearly being completely replaced.

                    – Philip Couling
                    2 hours ago















                    This is not the method that VIM uses by default. The doco (:help :write) states that it temporarily changes permissions, and a quick truss confirms that the doco is right. You have non-default backup options set.

                    – JdeBP
                    2 hours ago





                    This is not the method that VIM uses by default. The doco (:help :write) states that it temporarily changes permissions, and a quick truss confirms that the doco is right. You have non-default backup options set.

                    – JdeBP
                    2 hours ago













                    The phrase "by default" means "what happens if not otherwise configired" no? And :set backupcopy? results in backupcopy=auto. I've checked my config. There's nothing in there to change this behaviour (I think?)

                    – Philip Couling
                    2 hours ago







                    The phrase "by default" means "what happens if not otherwise configired" no? And :set backupcopy? results in backupcopy=auto. I've checked my config. There's nothing in there to change this behaviour (I think?)

                    – Philip Couling
                    2 hours ago















                    when vim is in compatible mode, :w! will only override the read-only mode of the buffer, but will not try to change the file perms back and forth or do other amateur hour stuff. This is the only right behavior -- it would be nice if it was clearly mentioned which one of the options turned on or off by :set compatible is responsible for this.

                    – mosvy
                    1 hour ago





                    when vim is in compatible mode, :w! will only override the read-only mode of the buffer, but will not try to change the file perms back and forth or do other amateur hour stuff. This is the only right behavior -- it would be nice if it was clearly mentioned which one of the options turned on or off by :set compatible is responsible for this.

                    – mosvy
                    1 hour ago











                    1














                    When you do w! in Vim, what actually happens depends on who owns the file.




                    • If you (the current user) are the owner of the file, Vim will change the permissions to be writable before rewriting the file. It then removes the write permissions to restore the permission bits to what it was from the start.


                    • If you are not the owner of the file, but if you have write permissions in the current directory, Vim will delete the original file and write the document to a new file with the same name. The new file will then be assigned the same permissions as the original file, but will be owned by you.





                    This is the first case (output from ktrace+kdump on OpenBSD):



                    13228 vim      CALL  chmod(0x19b1d94b4b10,0100644<S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH|S_IFREG>)
                    13228 vim NAMI "file"
                    13228 vim RET chmod 0
                    13228 vim CALL lseek(3,0x1000,SEEK_SET)
                    13228 vim RET lseek 4096/0x1000
                    13228 vim CALL write(3,0x19b1e0aa9000,0x1000)


                    This changes permissions on the file so that it's writable (the S_IWUSR flag used with chmod()) and writes the buffer to it.



                    It then sets the original permissions:



                    13228 vim      CALL  fchmod(4,0100444<S_IRUSR|S_IRGRP|S_IROTH|S_IFREG>)
                    13228 vim RET fchmod 0
                    13228 vim CALL close(4)
                    13228 vim RET close 0


                    For the other case:



                    It first unlinks (deletes) the file and then recreates it (before writing to the file and changing permissions later):



                    44487 vim      CALL  unlink(0x79fdbc1f000)
                    44487 vim NAMI "file"
                    44487 vim RET unlink 0
                    44487 vim CALL open(0x79fdbc1f000,0x201<O_WRONLY|O_CREAT>,0644<S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH>)
                    44487 vim NAMI "file"
                    44487 vim RET open 4





                    share|improve this answer


























                    • It's really odd that you would get different results (even on a different OS) for the same software. Could you confirm the version you are using and the value of the two relavent config options you mention (backup and backupcopy)

                      – Philip Couling
                      1 hour ago











                    • @PhilipCouling Vim 8.1.800. Both backup and backupcopy are unset (this is without a private .vimrc file).

                      – Kusalananda
                      1 hour ago











                    • Likewise neither of mine are set in config and 8.1.800 is very close to 8.1.875, could you also confirm the result of set backup? and set backupcopy? It's plausible the good people at Debian compiled in something different.

                      – Philip Couling
                      1 hour ago








                    • 1





                      I did a truss on FreeBSD of both VIM and NeoVIM. I even checked nvi for good measure. I duplicated Kusalananda's results with all three.

                      – JdeBP
                      1 hour ago






                    • 1





                      @JdeBP Yes I'm rapidly becoming suspicious I'm seeing something unusual or Debian specific.

                      – Philip Couling
                      1 hour ago
















                    1














                    When you do w! in Vim, what actually happens depends on who owns the file.




                    • If you (the current user) are the owner of the file, Vim will change the permissions to be writable before rewriting the file. It then removes the write permissions to restore the permission bits to what it was from the start.


                    • If you are not the owner of the file, but if you have write permissions in the current directory, Vim will delete the original file and write the document to a new file with the same name. The new file will then be assigned the same permissions as the original file, but will be owned by you.





                    This is the first case (output from ktrace+kdump on OpenBSD):



                    13228 vim      CALL  chmod(0x19b1d94b4b10,0100644<S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH|S_IFREG>)
                    13228 vim NAMI "file"
                    13228 vim RET chmod 0
                    13228 vim CALL lseek(3,0x1000,SEEK_SET)
                    13228 vim RET lseek 4096/0x1000
                    13228 vim CALL write(3,0x19b1e0aa9000,0x1000)


                    This changes permissions on the file so that it's writable (the S_IWUSR flag used with chmod()) and writes the buffer to it.



                    It then sets the original permissions:



                    13228 vim      CALL  fchmod(4,0100444<S_IRUSR|S_IRGRP|S_IROTH|S_IFREG>)
                    13228 vim RET fchmod 0
                    13228 vim CALL close(4)
                    13228 vim RET close 0


                    For the other case:



                    It first unlinks (deletes) the file and then recreates it (before writing to the file and changing permissions later):



                    44487 vim      CALL  unlink(0x79fdbc1f000)
                    44487 vim NAMI "file"
                    44487 vim RET unlink 0
                    44487 vim CALL open(0x79fdbc1f000,0x201<O_WRONLY|O_CREAT>,0644<S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH>)
                    44487 vim NAMI "file"
                    44487 vim RET open 4





                    share|improve this answer


























                    • It's really odd that you would get different results (even on a different OS) for the same software. Could you confirm the version you are using and the value of the two relavent config options you mention (backup and backupcopy)

                      – Philip Couling
                      1 hour ago











                    • @PhilipCouling Vim 8.1.800. Both backup and backupcopy are unset (this is without a private .vimrc file).

                      – Kusalananda
                      1 hour ago











                    • Likewise neither of mine are set in config and 8.1.800 is very close to 8.1.875, could you also confirm the result of set backup? and set backupcopy? It's plausible the good people at Debian compiled in something different.

                      – Philip Couling
                      1 hour ago








                    • 1





                      I did a truss on FreeBSD of both VIM and NeoVIM. I even checked nvi for good measure. I duplicated Kusalananda's results with all three.

                      – JdeBP
                      1 hour ago






                    • 1





                      @JdeBP Yes I'm rapidly becoming suspicious I'm seeing something unusual or Debian specific.

                      – Philip Couling
                      1 hour ago














                    1












                    1








                    1







                    When you do w! in Vim, what actually happens depends on who owns the file.




                    • If you (the current user) are the owner of the file, Vim will change the permissions to be writable before rewriting the file. It then removes the write permissions to restore the permission bits to what it was from the start.


                    • If you are not the owner of the file, but if you have write permissions in the current directory, Vim will delete the original file and write the document to a new file with the same name. The new file will then be assigned the same permissions as the original file, but will be owned by you.





                    This is the first case (output from ktrace+kdump on OpenBSD):



                    13228 vim      CALL  chmod(0x19b1d94b4b10,0100644<S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH|S_IFREG>)
                    13228 vim NAMI "file"
                    13228 vim RET chmod 0
                    13228 vim CALL lseek(3,0x1000,SEEK_SET)
                    13228 vim RET lseek 4096/0x1000
                    13228 vim CALL write(3,0x19b1e0aa9000,0x1000)


                    This changes permissions on the file so that it's writable (the S_IWUSR flag used with chmod()) and writes the buffer to it.



                    It then sets the original permissions:



                    13228 vim      CALL  fchmod(4,0100444<S_IRUSR|S_IRGRP|S_IROTH|S_IFREG>)
                    13228 vim RET fchmod 0
                    13228 vim CALL close(4)
                    13228 vim RET close 0


                    For the other case:



                    It first unlinks (deletes) the file and then recreates it (before writing to the file and changing permissions later):



                    44487 vim      CALL  unlink(0x79fdbc1f000)
                    44487 vim NAMI "file"
                    44487 vim RET unlink 0
                    44487 vim CALL open(0x79fdbc1f000,0x201<O_WRONLY|O_CREAT>,0644<S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH>)
                    44487 vim NAMI "file"
                    44487 vim RET open 4





                    share|improve this answer















                    When you do w! in Vim, what actually happens depends on who owns the file.




                    • If you (the current user) are the owner of the file, Vim will change the permissions to be writable before rewriting the file. It then removes the write permissions to restore the permission bits to what it was from the start.


                    • If you are not the owner of the file, but if you have write permissions in the current directory, Vim will delete the original file and write the document to a new file with the same name. The new file will then be assigned the same permissions as the original file, but will be owned by you.





                    This is the first case (output from ktrace+kdump on OpenBSD):



                    13228 vim      CALL  chmod(0x19b1d94b4b10,0100644<S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH|S_IFREG>)
                    13228 vim NAMI "file"
                    13228 vim RET chmod 0
                    13228 vim CALL lseek(3,0x1000,SEEK_SET)
                    13228 vim RET lseek 4096/0x1000
                    13228 vim CALL write(3,0x19b1e0aa9000,0x1000)


                    This changes permissions on the file so that it's writable (the S_IWUSR flag used with chmod()) and writes the buffer to it.



                    It then sets the original permissions:



                    13228 vim      CALL  fchmod(4,0100444<S_IRUSR|S_IRGRP|S_IROTH|S_IFREG>)
                    13228 vim RET fchmod 0
                    13228 vim CALL close(4)
                    13228 vim RET close 0


                    For the other case:



                    It first unlinks (deletes) the file and then recreates it (before writing to the file and changing permissions later):



                    44487 vim      CALL  unlink(0x79fdbc1f000)
                    44487 vim NAMI "file"
                    44487 vim RET unlink 0
                    44487 vim CALL open(0x79fdbc1f000,0x201<O_WRONLY|O_CREAT>,0644<S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH>)
                    44487 vim NAMI "file"
                    44487 vim RET open 4






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 23 mins ago

























                    answered 2 hours ago









                    KusalanandaKusalananda

                    132k17252415




                    132k17252415













                    • It's really odd that you would get different results (even on a different OS) for the same software. Could you confirm the version you are using and the value of the two relavent config options you mention (backup and backupcopy)

                      – Philip Couling
                      1 hour ago











                    • @PhilipCouling Vim 8.1.800. Both backup and backupcopy are unset (this is without a private .vimrc file).

                      – Kusalananda
                      1 hour ago











                    • Likewise neither of mine are set in config and 8.1.800 is very close to 8.1.875, could you also confirm the result of set backup? and set backupcopy? It's plausible the good people at Debian compiled in something different.

                      – Philip Couling
                      1 hour ago








                    • 1





                      I did a truss on FreeBSD of both VIM and NeoVIM. I even checked nvi for good measure. I duplicated Kusalananda's results with all three.

                      – JdeBP
                      1 hour ago






                    • 1





                      @JdeBP Yes I'm rapidly becoming suspicious I'm seeing something unusual or Debian specific.

                      – Philip Couling
                      1 hour ago



















                    • It's really odd that you would get different results (even on a different OS) for the same software. Could you confirm the version you are using and the value of the two relavent config options you mention (backup and backupcopy)

                      – Philip Couling
                      1 hour ago











                    • @PhilipCouling Vim 8.1.800. Both backup and backupcopy are unset (this is without a private .vimrc file).

                      – Kusalananda
                      1 hour ago











                    • Likewise neither of mine are set in config and 8.1.800 is very close to 8.1.875, could you also confirm the result of set backup? and set backupcopy? It's plausible the good people at Debian compiled in something different.

                      – Philip Couling
                      1 hour ago








                    • 1





                      I did a truss on FreeBSD of both VIM and NeoVIM. I even checked nvi for good measure. I duplicated Kusalananda's results with all three.

                      – JdeBP
                      1 hour ago






                    • 1





                      @JdeBP Yes I'm rapidly becoming suspicious I'm seeing something unusual or Debian specific.

                      – Philip Couling
                      1 hour ago

















                    It's really odd that you would get different results (even on a different OS) for the same software. Could you confirm the version you are using and the value of the two relavent config options you mention (backup and backupcopy)

                    – Philip Couling
                    1 hour ago





                    It's really odd that you would get different results (even on a different OS) for the same software. Could you confirm the version you are using and the value of the two relavent config options you mention (backup and backupcopy)

                    – Philip Couling
                    1 hour ago













                    @PhilipCouling Vim 8.1.800. Both backup and backupcopy are unset (this is without a private .vimrc file).

                    – Kusalananda
                    1 hour ago





                    @PhilipCouling Vim 8.1.800. Both backup and backupcopy are unset (this is without a private .vimrc file).

                    – Kusalananda
                    1 hour ago













                    Likewise neither of mine are set in config and 8.1.800 is very close to 8.1.875, could you also confirm the result of set backup? and set backupcopy? It's plausible the good people at Debian compiled in something different.

                    – Philip Couling
                    1 hour ago







                    Likewise neither of mine are set in config and 8.1.800 is very close to 8.1.875, could you also confirm the result of set backup? and set backupcopy? It's plausible the good people at Debian compiled in something different.

                    – Philip Couling
                    1 hour ago






                    1




                    1





                    I did a truss on FreeBSD of both VIM and NeoVIM. I even checked nvi for good measure. I duplicated Kusalananda's results with all three.

                    – JdeBP
                    1 hour ago





                    I did a truss on FreeBSD of both VIM and NeoVIM. I even checked nvi for good measure. I duplicated Kusalananda's results with all three.

                    – JdeBP
                    1 hour ago




                    1




                    1





                    @JdeBP Yes I'm rapidly becoming suspicious I'm seeing something unusual or Debian specific.

                    – Philip Couling
                    1 hour ago





                    @JdeBP Yes I'm rapidly becoming suspicious I'm seeing something unusual or Debian specific.

                    – Philip Couling
                    1 hour ago


















                    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%2f502826%2fhow-vim-overwrites-readonly-mode%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

                    Szabolcs (Ungheria) Altri progetti | Menu di navigazione48°10′14.56″N 21°29′33.14″E /...

                    Discografia di Klaus Schulze Indice Album in studio | Album dal vivo | Singoli | Antologie | Colonne...

                    How to make inet_server_addr() return localhost in spite of ::1/128RETURN NEXT in Postgres FunctionConnect to...