au BufReadPost * if &readonly | set nomodifiable

218 views
Skip to first unread message

rudy_b

unread,
May 5, 2010, 12:43:51 AM5/5/10
to vim...@googlegroups.com

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.

I tried to play around with the different au[tocmd] events
(http://www.at.vim.org/html/autocmd.html#BufReadCmd)
But, seems that I cannot get around the issue that I have.

Can any one give me some solution around it please.
I am just very annoyed when gvim asks you for a permission to modify a
readonly file. I would like for "nomodifable" to be set all the time for
readonly files.

please advise!?!

thanks in advance,
Rudy

--
View this message in context: http://old.nabble.com/au-BufReadPost-*-if--readonly-%7C-set-nomodifiable-tp28456618p28456618.html
Sent from the Vim - General mailing list archive at Nabble.com.

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

Maxim Kim

unread,
May 5, 2010, 2:24:16 AM5/5/10
to vim_use

On 5 май, 08:43, 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.

I have just tried your autocommand and it works for me with 2 readonly
buffers.

Gary Johnson

unread,
May 5, 2010, 10:30:02 AM5/5/10
to vim...@googlegroups.com
On 2010-05-04, rudy_b wrote:
> Hi,
> I have included the autocmd (listed below) in my .vimrc file.
> au BufReadPost * if &readonly | set nomodifiable

I haven't tried your command, but you're missing an endif. It
should be

au BufReadPost * if &readonly | set nomodifiable | endif

HTH,
Gary

Ben Fritz

unread,
May 5, 2010, 11:30:49 AM5/5/10
to vim_use


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.

rudy_b

unread,
May 5, 2010, 11:54:30 AM5/5/10
to vim...@googlegroups.com

hi,
how did you open your second file? Cause it doesn't work for me.
I am using either :vsplit or :sp and non works
--
View this message in context: http://old.nabble.com/au-BufReadPost-*-if--readonly-%7C-set-nomodifiable-tp28456618p28462694.html
Sent from the Vim - General mailing list archive at Nabble.com.

rudy_b

unread,
May 5, 2010, 4:40:17 PM5/5/10
to vim...@googlegroups.com

thanks for your responses, but I tried everything and it still doen't work.

I tired this:
au BufReadPost * if &readonly | set nomodifiable | endif

and this:
autocmd BufRead,BufWinEnter * if &ft!='qf' | let &l:modifiable = (&readonly
? 0 : 1) | endif

It only works fine for the very first buffer that I open in a window, but
once I open a new readonly file (using "vsplit" or "sp") it just allows for
modification....

Any idea why this is happenning? or any possible solution around it?

thanks,
Rudy
--
View this message in context: http://old.nabble.com/au-BufReadPost-*-if--readonly-%7C-set-nomodifiable-tp28456618p28466220.html
Sent from the Vim - General mailing list archive at Nabble.com.

Maxim Kim

unread,
May 6, 2010, 1:44:27 AM5/6/10
to vim_use


On 5 май, 19:54, rudy_b <rudyke...@yahoo.com> wrote:
> hi,
> how did you open your second file? Cause it doesn't work for me.
> I am using either :vsplit or :sp   and non works

I did it with :e command. I have also tried :sp -- it works just fine.

Could you show us your .vimrc?

Ben Fritz

unread,
May 6, 2010, 10:04:09 AM5/6/10
to vim_use


On May 5, 3:40 pm, rudy_b <rudyke...@yahoo.com> wrote:
> thanks for your responses, but I tried everything and it still doen't work.
>
> I tired this:
> au BufReadPost * if &readonly | set nomodifiable | endif
>
> and this:
> autocmd BufRead,BufWinEnter * if &ft!='qf' | let &l:modifiable = (&readonly
> ? 0 : 1) | endif
>
> It only works fine for the very first buffer that I open in a window, but
> once I open a new readonly file (using "vsplit" or "sp") it just allows for
> modification....
>
> Any idea why this is happenning? or any possible solution around it?
>

What version of Vim are you using? The second one (which I supplied)
works fine in pretty much all cases for me. I open files using :vsp
filename, :new filename, :tabe filename, :e filename, :tabnew
filename, :drop filename, :tab drop filename, :sp and then :e
filename, drag-and-drop, and using Windows file associations all the
time. I can't think of anything that would interfere with something so
simple.

There is this, from :help BufWinEnter:

Does not happen for |:split| without
arguments, since you keep editing the same
buffer, or ":split" with a file that's already
open in a window, because it re-uses an
existing buffer. But it does happen for a
":split" with the name of the current buffer,
since it reloads that buffer.

However, using :split without a file name, followed by ":e filename",
should work fine, as the BufRead event will be triggered.

It may also be a good debugging idea, if you put the autocmd we
discussed, into its own file, say readonly.vim, and put this file in
your plugin directory. This way, when you load your new file, you can
":verbose set modifiable?" to see where the option was last set for
this particular file. It should point to readonly.vim. Otherwise, you
have something else setting this option.

Additionally, something I DIDN'T include in my original email, was
that I surround this particular autocmd (among others) with:

augroup file_handling
au!
...
...
augroup END

This (among other things I do) ensures that I can just :source
my .vimrc again to see any updates if I'm making changes. The SAFEST
way to test would be to restart Vim entirely. But you at least need to
do this if you're not restarting Vim between edits of your .vimrc.

Ben Fritz

unread,
May 6, 2010, 10:11:10 AM5/6/10
to vim_use


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

By the way, the reason I use this behavior, is that I use a version-
control system at work that requires a lock of a file before making
any changes. Without the lock, the file shows as readonly. Before I
added the autocmd I gave (not the broken one above!), I would often
make changes, fail to notice the warning that I was modifying a
readonly file, and go to :w, which would always fail. :w! doesn't even
work, since I don't have a lock. Acquiring the lock makes Vim thinks
the file has changed (and indeed, since I don't have a lock, it may
have changed in truth), so I found it best just to always set
nomodifiable until I have the lock.

rudy_b

unread,
May 6, 2010, 2:22:41 PM5/6/10
to vim...@googlegroups.com

Hey guys thanks for your replies.
Still Doesn't work.
My gvim version is 7.2.
Aince at work I am using a revision control too, that is why having this
thing fixed is very helpful for me.
I almost shrunk my .vimrc down to this:

----------------------------------------
:set nocp
:set ru

" au BufReadPost * if &readonly | set nomodifiable | endif
autocmd BufRead,BufWinEnter * if &ft!='qf' | let &l:modifiable =
(&readonly ? 0 : 1) | endif
---------------
And I used the last two lines interchangably.
But still not good.

But, I figured something intresting.
I think what is going on (at least from my observation) is that this
specific setting is buffer related setting and not a file related. I tried
using split function with the name of the second file next to it:
:split read_only_file.txt
and this worked.
BUT, if I do
:split .
(which is what I usually do) that opens up the directory, where I can select
my file, then it is hosed up.

Then I thought that okay at least I know if I put the name of the file next
to that split command, then it will work.
BUT, another wired thing is that if I do the same thing on a writable file:
:split writable_file.txt
and when I try to modify it, it DOENS'T work. meaning that I cannot write
into that file.

conclusion:
this is what is going on:

If I use (e: new_file) or (split: new_file) or (vsplit: new_file), then:
Regardless of the writability/readability privileges, I CANNOT modify the
file. In other words, "nonmodifiable" is set.

If I use (e: .) or (split: .) or (vsplit: .), then:
I can select my other file, but again, regardless of the
writability/readability privileges, I CAN modify the files. In other words,
"modifiable" is set.

Any idea, why?

Thanks for your time,

--Rudy
--
View this message in context: http://old.nabble.com/au-BufReadPost-*-if--readonly-%7C-set-nomodifiable-tp28456618p28477764.html
Sent from the Vim - General mailing list archive at Nabble.com.

Ben Fritz

unread,
May 6, 2010, 4:26:30 PM5/6/10
to vim_use, drc...@campbellfamily.biz
Wow, HEY! I also see the autocmd not working, if I open the file from
the netrw directory exploration window. In fact, the readonly option
does not even get set!

If I then use :e in the new buffer (with no arguments) to reload the
buffer, the readonly and modifiable options get set as desired.

I tried with the .vimrc you sent. I did not try disabling all plugins
besides netrw, but I don't use very many of them.

The autocmd works fine using splits with your .vimrc and no plugins at
all. This means the directory listing won't work.

> Then I thought that okay at least I know if I put the name of the file next
> to that split command, then it will work.
> BUT, another wired thing is that if I do the same thing on a writable file:
> :split writable_file.txt
> and when I try to modify it, it DOENS'T work. meaning that I cannot write
> into that file.
>

This, I cannot reproduce.

I opened writeable_file, modifiable was set.
I :sp nonwriteable_file, nomodifiable was set.
I :sp writeable_file2, modifiable was set.

This works even when the writeable files do not exist for me.

All this was done with no plugins, and the .vimrc you supplied at the
top of this post.

> conclusion:
> this is what is going on:
>
> If I use (e: new_file) or (split: new_file) or (vsplit: new_file), then:
> Regardless of the writability/readability privileges, I CANNOT modify the
> file. In other words, "nonmodifiable" is set.

Did you make sure to close down Vim, and launch a NEW vim, with the
new autocmd between loads?

>
> If I use (e: .) or (split: .) or (vsplit: .), then:
> I can select my other file, but again, regardless of the
> writability/readability privileges, I CAN modify the files. In other words,
> "modifiable" is set.
>

I can duplicate this. In addition, noreadonly is set regardless of the
file properties.


I am using Vim 7.2.411 on Windows XP.

My netrw plugin starts with:

" netrwPlugin.vim: Handles file transfer and remote directory listing
across a network
" PLUGIN SECTION
" Date: Aug 10, 2008
" Maintainer: Charles E Campbell, Jr <NdrO...@ScampbellPfamily.AbizM-
NOSPAM>
" GetLatestVimScripts: 1075 1 :AutoInstall: netrw.vim


As a workaround, immediately after loading a file from NetRW, do an :e
with no arguments to reload the file and set the appropriate readonly/
modifiable options based on file attributes. I do not know why this is
not currently working.

Tony Mechelynck

unread,
Jun 29, 2010, 3:24:46 AM6/29/10
to vim...@googlegroups.com, Ben Fritz, drc...@campbellfamily.biz, rudy...@yahoo.com
On 06/05/10 22:26, Ben Fritz wrote:
>
>
> On May 6, 1:22 pm, rudy_b<rudyke...@yahoo.com> wrote:
>> Hey guys thanks for your replies.
>> Still Doesn't work.
>> My gvim version is 7.2.
>> Aince at work I am using a revision control too, that is why having this
>> thing fixed is very helpful for me.
>> I almost shrunk my .vimrc down to this:
>>
>> ----------------------------------------
>> :set nocp
>> :set ru
>>
>> " au BufReadPost * if&readonly | set nomodifiable | endif
>> autocmd BufRead,BufWinEnter * if&ft!='qf' | let&l:modifiable =

Hm. Just a wild thought (untested): try the following (which assumes
'nocompatible'):


if has('autocmd')
au VimEnter * augroup nomodif
au VimEnter * au BufReadPost,BufNewFile * if &buftype == ""
\ | let &l:ma = &l:ro | endif
au VimEnter * augroup END
endif


Then restart Vim (every time you make changes to your vimrc, and after
saving your changes) in order to test it.

If it doesn't work: while editing a file where 'modifiable' is not what
you expect:

:verbose setlocal ma? ro? bt?


Best regards,
Tony.
--
A disciple of another sect once came to Drescher as he was
eating his morning meal. "I would like to give you this personality
test", said the outsider, "because I want you to be happy."
Drescher took the paper that was offered him and put it into
the toaster -- "I wish the toaster to be happy too".

Benjamin Fritz

unread,
Jun 29, 2010, 2:08:39 PM6/29/10
to vim_use
On Tue, Jun 29, 2010 at 2:24 AM, Tony Mechelynck
<antoine.m...@gmail.com> wrote:
> Hm. Just a wild thought (untested): try the following (which assumes
> 'nocompatible'):
>
>
>  if has('autocmd')
>        au VimEnter * augroup nomodif
>        au VimEnter * au BufReadPost,BufNewFile * if &buftype == ""
>                \ | let &l:ma = &l:ro | endif
>        au VimEnter * augroup END
>  endif
>
>

Won't work. The problem seen here was a netrw bug that prevented the
'readonly' option from being set properly. This autocmd will still
fail because the readonly option is not being set.

I believe this issue in netrw was fixed, I vaguely remember reading
something on vim_dev. But, I don't recall the version it was fixed in.

Chip?

Charles Campbell

unread,
Jul 8, 2010, 3:22:24 PM7/8/10
to vim...@googlegroups.com
Benjamin Fritz wrote:
> On Tue, Jun 29, 2010 at 2:24 AM, Tony Mechelynck
> <antoine.m...@gmail.com> wrote:
>
>> Hm. Just a wild thought (untested): try the following (which assumes
>> 'nocompatible'):
>>
>>
>> if has('autocmd')
>> au VimEnter * augroup nomodif
>> au VimEnter * au BufReadPost,BufNewFile * if&buftype == ""
>> \ | let&l:ma =&l:ro | endif

>> au VimEnter * augroup END
>> endif
>>
>>
>>
> Won't work. The problem seen here was a netrw bug that prevented the
> 'readonly' option from being set properly. This autocmd will still
> fail because the readonly option is not being set.
>
> I believe this issue in netrw was fixed, I vaguely remember reading
> something on vim_dev. But, I don't recall the version it was fixed in.
>
> Chip?
>
Just noticed this (almost didn't, I was about to delete old email) --
and I already had cleaned out most of the original part of the thread.

Anyway, I tried

au BufReadPost * if &readonly | set nomodifiable | endif

with the latest netrw (v139e) and if a file was r--r--r-- (ie. read
only), then it got marked nomodifiable. So may I suggest to the OP that
he use http://mysite.verizon.net/astronaut/vim/index.html#NETRW .

HTH,
Chip Campbell

Reply all
Reply to author
Forward
0 new messages