Vim: sync read-only state between buffer and file on disk

I work with Perforce and all my client files are read-only by default. So when I view the source code in vim buffers, they are also marked as read-only (maybe this is not the exact wording: I can edit its contents, but I cannot save it without the "!"). At some point I start making some changes and find that I have to check the file in Perforce. I have a "good" command for this:

command PE !p4 edit % 

      

But after it ends, vim offers me a choice:

  • Download content from disk (which I don't want to do because I lost my rights).
  • Or keep the buffer as it is, but that keeps the read-only mode (which I don't want either).

Of course I can change RO manually by doing :set noro

, but obviously I want to avoid that. I have currently added it to the command PE

above, but this does not check the actual status of the file, so it seems a little dangerous:

command PE !p4 edit % | set noro

      

What I really want is "true" read-only state synchronization between a file on disk and a buffer in vim.


UPD: Tricked, the latest version works out of the box - if the p4 edit

condition of the RO state in the buffer fails, it will not be deleted. No idea how it works ...

+3


source to share


1 answer


Vim has a function filewritable()

to query the access status of a file, why don't you use that?



command PE execute '!p4 edit %' | let &readonly = !filewritable(expand('%'))

      

+1


source







All Articles