I've run into problems a few times because vim's encoding was set to latin1 by default and I didn't notice and assumed it was using utf-8. Now that I have, I'd like to set up vim so that it will do the right thing in all obvious cases, and use utf-8 by default.
What I'd like to avoid:
What I've got 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 the bomb
from setglobal fileencoding
to its own statement because otherwise it doesn't actually work. I also commented out that line because of my uncertainty towards BOMs.
What I'm looking for:
Ultimately, I'd like this to result in a no-thought-required copy/paste snippet that will set up vim for utf-8-by-default that will work across platforms.
EDIT: I've marked my own answer as accepted for now, as far as I can tell it works okay and accounts for all things it can reasonably account for. But it's not set in stone; if you have any new information please feel free to answer!
utf-8 . Now Vim will use UTF-8 to represent the text in the buffer. Plus, it will also make a more determined effort to detect the UTF-8 encoding in a file. Besides looking for a byte-order mark, it will also check for UTF-8 without a byte-order mark before falling back to Latin-1.
set encoding=utf-8 " The encoding displayed. set fileencoding=utf-8 " The encoding written to file. You may as well set both in your ~/. vimrc if you always want to work with utf-8.
Vim supports many character encodings, and provides enhanced functionality to work with some of them. For example, Vim allows you to change the character encoding of a particular file, check for characters that aren't valid for a particular encoding, and find the code value for a particular character.
In response to sehe, I'll give a go at answering my own question! I removed the updates I made to the original question and have moved them to this answer. This is probably the better way to do it.
The 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 removed the bomb
line because according to the BOM Wikipedia page it is not needed when using utf-8 and in fact defeats ASCII backwards compatibility. As long as ucs-bom
is first in fileencodings
, vim will be able to detect and handle existing files with BOMs, so it is not needed for that either.
I removed the fileencodings
line because it is not needed in this case. From the Vim docs: When 'encoding' is set to a Unicode encoding, and 'fileencodings' was not set yet, the default for 'fileencodings' is changed.
I am using setglobal filencoding
(as opposed to set fileencoding
) because:
When reading a file, fileencoding
will be automatically set based on fileencodings
. So it only matters for new files then. And according to the docs again:
For a new file the global value of 'fileencoding' is used.
I think it would suffice to have a vanilla vimrc + fenc=utf-8
The rest should be pretty decent out-of-the-box
I'd use the BOM only on Windows platforms with Microsoft tooling (although even some of these fail to always write a BOM; however it is the default for Notepad Unicode saving, .NET XmlWriter and other central points of the MS platform tools)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With