When using Vim to edit a read only file it will just provide a warning the first time it is edited but allow any changes to take place. I can understand this behaviour could be useful for someone intending on saving the file under another name; but I sometimes open files that I don't have write access to, then forget and make changes.

Is it possible to enter a mode where Vim will allow the file to be viewed but block any option that makes changes?


I found a solution. Setting nomodifiable prevents the file being edited (as used in help windows). I created a simple function to set or unset modifiable depending on if readonly is set and attached it to an autocmd.

" Don't allow editing of read only files
autocmd BufRead * call RONoEdit()

function! RONoEdit()
  if &readonly == 1
    set nomodifiable
    set modifiable
  • 4
    You can make it shorter by doing: let &modifiable = !&readonly – Martin Tournoij May 29 '15 at 9:10
  • @Carpetsmoker Much better as a one liner, thanks! However it would still have to be: autocmd BufRead * let &modifiable = !&readonly – ZeroKelvinKeyboard May 29 '15 at 15:11
  • With this, after opening a readonly file and then creating a new buffer the new buffer ends up nomodifiable. – Praxeolitic Jul 24 '15 at 5:13
  • Thanks for pointing that out, I hadn't noticed. I'm not sure how to test for a new buffer. – ZeroKelvinKeyboard Jul 24 '15 at 7:22
  • 1
    @Praxeolitic: I just posted an answer which should address this, let me know if it works. – s4y Nov 11 '16 at 22:16

Add this to your .vimrc:

autocmd BufRead * let &l:modifiable = !&readonly
  • This builds on @ZeroKelvinKeyboard's answer to only affect the current buffer. – s4y Nov 11 '16 at 21:58

As much as the answers above are correct, there is another thing to take into account: as long as the file is "read-only", there should be no way of changing the file's content unless you're the owner of the file or have the right to make that file writable. And even then vim doesn't write changes to the file unless you explicitly override the "read-only" state by ":w!".

If you can in fact edit a file which is supposed to be read-only, you may want to recheck the rights, there may be a security misconfiguration. If you only make vim unable to edit the file, other ways of editing will still be in place so if the file is not to be changed, it should have the rights set properly. In windows, you can force the file to be read-only by explicitly denying the change right to everyone, in Linux you can use the "immutable" extended attribute (chattr +i).

  • 2
    Sorry if I was not very clear. This question was not about accidentally changing a read only file; I wanted to stop myself from being able to edit the buffer of a file that I did not have write access for. Sometimes I accidentally open system configuration files as a regular user rather than the root user. In this case I don't have write access to the file, so the buffer appears as read only but Vim will still allow me to edit the buffer. When I try to save I cannot because I don't have write access. I wanted Vim to block changes to the buffer in this case. – ZeroKelvinKeyboard Jun 1 '15 at 13:52
  • Ah, I see. I got the idea that you wanted this as a failsafe. Disregard my answer then :) – mikky Jun 1 '15 at 18:34
  • OP means that when one opens a not-read-only file in the OS, but he/she currently doesn't want to (or consider appropriate to) make change, and wants to restrict himself not to. I am thinking exactly the same, thus going here from Google! – Violapterin Dec 27 '16 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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