4

I would like to set some options for all things I will read but not modify. That is special settings for read-only files (and files in vim view mode).

Particularly I'm interested in setlocal wrap for read-only files.


I know that for special type files (like quickfix) I can use autocmd.

5

I think, you can use an BufEnter autocommand, that checks, whether the file is modifiable. Something like this should work (untested):

augroup ReadOnly
    au!
    au BufEnter *  if (!&modifiable || &ro) | setl wrap | endif
augroup END
  • yeah, works, briliant! I will learn more about autocmd, thanks! what is difference between !&modifiable and &ro ? – Mesco Aug 6 '16 at 13:45
  • 1
    For windows that appear without entering like quickfix BufRead could be better choice. The list of autocommands: vimdoc.sourceforge.net/htmldoc/autocmd.html – Mesco Jan 21 '17 at 11:54
  • This answer does not distinguish file buffers from buffers created by :help or NERDTree; potentially breaking these latter. – Serge Stroobandt Sep 26 '18 at 14:12
  • well, first of all, that was not asked for. Second of all, you don't need the VimEnter autocommand for that. Rather check the buftype option. – Christian Brabandt Sep 26 '18 at 14:19
  • @ChristianBrabandt The other commenter, Mesco, is in fact issuing the very same warning, although stated in a different wording. I experienced unintended side effects from BufEnter * if (!&modifiable || &ro) with the before mentioned plugins first hand, albeit with a different payload (not wrap). Thanks for down-voting, by the way. – Serge Stroobandt Oct 2 '18 at 21:26
-1

Without breaking :help, nor NERDTree

The problem with BufEnter * if (!&modifiable || &ro) or BufRead * if (!&modifiable || &ro) is that these do not distinguish file buffers from buffers created by :help or NERDTree; potentially breaking these latter.

Hence, below solution rather employs VimEnter for creating for example a Vim-based Markdown viewer or pager.

augroup ReadOnly
    au!
    au VimEnter * if (!&modifiable || &ro) | setl wrap | endif
augroup END

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.