Wikia

Vim Tips Wiki

Make buffer modifiable state match file readonly state

Talk0
1,610pages on
this wiki
Revision as of 06:15, July 13, 2012 by JohnBot (Talk | contribs)

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()

Comments

Why is the b:setmodifiable variable needed?

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

Did you mean:

autocmd BufReadPost * if &readonly | setlocal nomodofiable | 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.


Advertisement | Your ad here

Around Wikia's network

Random Wiki