Wikia

Vim Tips Wiki

Make buffer modifiable state match file readonly state

Talk0
1,612pages on
this wiki
Revision as of 17:48, April 29, 2013 by ArraAvakian (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Tip 1238 Printable Monobook Previous Next

created 2006 · complexity intermediate · author Daniel Harding · version 6.0


If a file on disk is read-only, I would prefer that Vim prevent me from modifying it, rather than giving an error message when I try to write out my changes. The modifiable option does exactly this - when off, it prevents changes from being made to the buffer. However, the modifiable option is on by default. Thus I use the following function to keep a buffer's modifiable state in sync with the underlying file's readonly state. It works especially well when the autoread option is enabled.

function UpdateModifiable()
  if !exists("b:setmodifiable")
    let b:setmodifiable = 0
  endif
  if &readonly
    if &modifiable
      setlocal nomodifiable
      let b:setmodifiable = 1
    endif
  else
    if b:setmodifiable
      setlocal modifiable
    endif
  endif
endfunction
autocmd BufReadPost * call UpdateModifiable()

CommentsEdit

Why is the b:setmodifiable variable needed?

autocmd BufReadPost * if &modifiable | setlocal nomodifiable | else | setlocal modifiable | endif

Did you mean:

autocmd BufReadPost * if &readonly | setlocal nomodifiable | else | setlocal modifiable | endif

I think b:setmodifiable is there to set &modifiable only if the &readonly previously triggered the script to set &nomodifiable . So, it will skip files that are &nomodifiable but not &readonly. Is that correct? I don't think that case happens much.


You are right about b:setmodifiable. It is to prevent setting &modifiable when reloading a file that is not &readonly, but which had &nomodifiable set manually. It may be overkill, but it still works correctly.


Around Wikia's network

Random Wiki