0

When I edit .vimrc (for example), I often want to view the file as write-protected.

noremap <C-a> : sview $HOME/.vimrc <CR> \| : set noma <CR>

sets the nonmodifiable property for the file in both the editing instance end the view.

Is the property set per file and not per buffer?

If that is the case, how can I set a file as nonmodifiable when switching to a view and unset that property when switching out from that same view?

I want to toggle the modifiable property for a file while editing it in another instance of vim. It is important that the property is noma.

2

So it looks like you haven't learnt the Vim terminology and that's the source of your confusion. Forget those "instances", "views" and such. It's all about "buffers" and "windows".

sets noma property for the file in both the editing instance end the view.

To put it properly: "noma is set for the buffer in both windows".

Let's read :h :sview: "same as :split". Now read :h split: "If it is not loaded in any buffer, it will be read. Else the new window will use the already loaded buffer". Now to :h 'ma': "local to buffer".

Doesn't it make the things clear? Both windows show the same buffer, hence there exists only one buffer-local option "modifiable".

This question is specific about how to set (or switch on and off) noma for a file while editing it in another instance of vim.

Again, it should be said as: "how to set noma in a specific window only"? But as we already know "modifiable" belongs to a buffer, not to a window. Hence, you can only set it independently if you have two different buffers. For example,

new
read ++edit $MYVIMRC
1delete_
setfiletype vim
set buftype=nofile
" or instead of the previous line:
"setlocal nomodified nomodifiable

Note that I wouldn't even bother with "noma", as it's a "nofile"-buffer. So even if you've modified it, it's not easy to write the contents to the disk drive. But, of course, you can also add setlocal noma and forbid all editing operations if you wish.

| improve this answer | |
  • Yes, it's true that vim-terminology is not really my domain (yet). But what you describe is not the effect I am looking for. In the view I want to be locked out from any attempt to make changes. – Swepter May 21 at 16:52
  • @Swepter Again, it's not a view, it's a window. And as I said, you can add setl noma to the bottom. – Matt May 21 at 17:39
  • Ahh, I understand. Thank you! One last question. What is that 1delete_ thing? – Swepter May 21 at 17:52
  • @Swepter In Vim every empty buffer has one extra (empty) line. This is to delete it (delete line #1 into register "underscore" aka "black hole"). – Matt May 21 at 19:25
1

One alternative option would be to use view or vim -R <filename>, which opens the file in read-only mode. Then edit it in another instance of vim.

| improve this answer | |
  • I want to have the property noma set for the readonly instance. . I stated that in the topic - but it got edited away. – Swepter May 21 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.