2

There is a tip on Vim Wiki about working with Unicode:

if has("multi_byte")
  if &encoding !~? '^u'
    if &termencoding == ""
      let &termencoding = &encoding
    endif
    set encoding=utf-8
  endif
  setglobal fileencoding=utf-8
  set fileencodings=ucs-bom,utf-8,latin1
endif

I spent a lot of time trying to understand the purpose of using setglobal fileencoding=utf-8 line and I still don't understand.

  • What is the possible purpose of specifying fileencoding here at all? (Why not leave it to be "inherited" from encoding for new buffers and from fileencodings when we open an existing file?)
  • And why setglobal instead of set?
2
  • 1
    what exactly are you missing from my answer? Sep 21 at 16:07
  • 1
    @ChristianBrabandt Nothing special, Christian. I just want to draw more attention because this snippet interests me for quite a long period of time.
    – jsv
    Sep 21 at 21:27
3
+150

I think the recommendation for setting fileencoding instead of having it inherit from the encoding setting, is more or less to have it explicit that you are preferring UTF-8. In addition, since this is a buffer-local option, you don't want to set this only for the currently edited buffer (which ever that may be at the time of reading your .vimrc initialization file) but to have it globally apply, therefore using setg instead of set.

And finally, it is recommended to not mess with the value of encoding option after it has been initially set, because this may invalidate all the currently stored information your Vim is currently using (that may be register values, variables and possibly even buffer content).

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.