Wednesday, May 5, 2010

Re: au BufReadPost * if &readonly | set nomodifiable

On May 4, 11:43 pm, rudy_b <rudyke...@yahoo.com> wrote:
> Hi,
> I have included the autocmd (listed below) in my .vimrc file.
>  au BufReadPost * if &readonly | set nomodifiable
>
> The intention is to not let gvim even modify the read only files, which
> works great with some downside.
>
> It only works when I just start a new gvim window!!! If, in the same window,
> I load a different buffer (by opening a new read only file) I have to go in
> and manually do "set nomodifiable"... which is very annoying.
>

Here is my autocmd for this purpose:

" set readonly files to also be non-modifiable by default, and
others to be
" modifiable by default
autocmd BufRead,BufWinEnter * if &ft!='qf' | let &l:modifiable =
(&readonly ? 0 : 1) | endif

There are a few improvements here:

1. There's an endif (as Gary points out)
2. I use the local value of modifiable, so that new buffers do not
inherit this value (note the use of &l:modifiable instead of just
&modifiable). You could do this in your own using setlocal, but then
you wouldn't get the next impromement:
3. By also setting 'modifiable' when readonly is NOT set, I can load a
new buffer in the same window, or reload the same buffer after
clearing the readonly option, and it will work as intended. Finally,
4. I don't set 'modifiable' for the quickfix window, which is marked
non-modifiable but not readonly (if I recall correctly).

I do not remember what the additional BufWinEnter event is for.
Probably I was having trouble with a corner case or two where BufRead
wasn't firing. A new buffer with no associated file, perhaps? Or maybe
it's for handling hidden buffers that don't get re-read but whose
'readonly' status may have changed.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: