Search code examples
vimnerdtreeneovimfile-encodings

How to ignore nerdtree buffer in vimrc settings?


I have the following line in my init.vim:

if !&readonly
  set fileencoding=utf-8
endif

Which sets the fileencoding to utf-8. It works fine, unless I have my cursor in a nerdtree window, in which case I get the following error:

enter image description here

I guess that it's trying to set the nerdtree window to utf-8. I think that I could solve this by checking for nerdtree in the if statement (so that it doesn't try to set fileencoding for nerdtree windows). How do I do that?


Solution

  • I've checked and the nerdtree window is : noreadonly & nomodifiable. The message which you've received tells you all. You cannot modify a window which is 'nomodifiable' even by setting an option on it. Try:

    if !&readonly && &modifiable
      set fileencoding=utf-8
    endif
    

    Other option is to remember the current state in some variable:

    if !&readonly
        let prev_modifiable = &modifiable
        set modifiable
        set fileencoding=utf-8
        let &modifiable=prev_modifiable
    endif