How to properly configure vim for utf-8 editing

I had problems several times because the vim encoding was set to latin1 by default, and I did not notice and suggested that it uses utf-8. Now that I have it, I would like to configure vim so that it does everything correctly in all obvious cases and uses utf-8 by default.

What I would like to avoid:

  • Forcing a file saved in some other encoding that would work before my changes open as utf-8, which will lead to gibberish.
  • Forcing a terminal that does not support multibyte characters (such as Windows XP) to try and display them anyway, resulting in gibberish.
  • Interaction with the ability of other programs to read or edit files (I have a (possibly unreasonable) aversion to using the default specification, because I do not understand how likely this will lead to the launch of other programs.)
  • Other questions that I don't know enough to guess about (but hopefully you do!)

What I have so far:

if has("multi_byte") if &termencoding == "" let &termencoding = &encoding endif set encoding=utf-8 " better default than latin1 setglobal fileencoding=utf-8 " change default file encoding when writing new files "setglobal bomb " use a BOM when writing new files set fileencodings=ucs-bom,utf-8,latin1 " order to check for encodings when reading files endif 

This is taken and slightly modified from the vim wiki . I moved bomb from setglobal fileencoding to my own statement, because otherwise it actually doesn't work. I also commented on this line due to my uncertainty regarding specifications.

What I'm looking for:

  • Possible errors to avoid what I missed
  • Problems with existing code
  • Links to everything already mentioned / already indicated

Ultimately, I would like this to lead to an unnecessary copy / paste fragment that will set vim to utf-8 by default, which will work on different platforms.

EDIT: I marked my own answer as it is accepted at the moment, as far as I can tell, it works fine and takes into account everything that it can reasonably take into account. But it is not set in stone; If you have any new information, feel free to reply!

+22
vim encoding unicode utf-8
Mar 29 '11 at 19:02
source share
2 answers

In response to this, I will give an answer to my question! I removed the updates I made to the original question and moved them to this answer. This is probably the best way to do this.

Answer:

 if has("multi_byte") if &termencoding == "" let &termencoding = &encoding endif set encoding=utf-8 " better default than latin1 setglobal fileencoding=utf-8 " change default file encoding when writing new files endif 

I deleted the bomb line because according to Vim docs : When 'encoding' is set to a Unicode encoding, and 'fileencodings' was not set yet, the default for 'fileencodings' is changed.

I use setglobal filencoding (as opposed to set fileencoding ) because: When reading a file, fileencoding will be automatically set based on fileencodings . Thus, it only matters for new files. And according to the docs :

For the new file, the global value is 'fileencoding'.

+24
Apr 26 '11 at 19:29
source share

I think it would be enough to have vanilla vimrc + fenc = utf-8

The rest should be pretty decent in size

I would use the specification only on Windows platforms with Microsoft tools (although even some of them do not always write the specification, however this is the default value for saving Unicode for Notepad, .NET XmlWriter and other central points of the MS platform tools)

+2
Mar 29 '11 at 19:23
source share



All Articles