Wikia

Vim Tips Wiki

Changes: Make buffer modifiable state match file readonly state

Edit

Back to page

(Change <tt> to <code>, perhaps also minor tweak.)
(Comments)
Line 37: Line 37:
   
 
<pre>
 
<pre>
autocmd BufReadPost * if &modifiable | setlocal nomodofiable | else | setlocal modifiable | endif
+
autocmd BufReadPost * if &modifiable | setlocal nomodifiable | else | setlocal modifiable | endif
 
</pre>
 
</pre>
   
Line 43: Line 43:
 
Did you mean:
 
Did you mean:
 
<pre>
 
<pre>
autocmd BufReadPost * if &readonly | setlocal nomodofiable | else | setlocal modifiable | endif
+
autocmd BufReadPost * if &readonly | setlocal nomodifiable | else | setlocal modifiable | endif
 
</pre>
 
</pre>
   

Revision as of 11:44, September 19, 2012

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