File Encodings in Vim

By default, You need to guide Vim to decode double-byte encodings like GBK and Big5. The default Vim configuration only works well with Unicode encodings including utf-8, utf-16, utf-16be etc..Edit your .vimrc file, add line like:

Now Vim is able to detect and decode GBK and Big5 encodings automatically. And according my experience, Vim respects utf-16 and utf-16be files only they have BOM byes. Otherwise, these files are wrongly decoded. In this case, you may want to manually reopen the file using a correct encoding. The Vim command like:

And Vim does not store BOM when saving by default. To enable/disable BOM saving, use:

I’ve attached a series of text files to learn the usage. These text file all contains string “123你好”, but saved in different encodings. Let’s list their code points first:

1 2 3
GBK 0x31 0x32 0x33 0xc4e3 0xbac3
Big5 0x31 0x32 0x33 0xa741 0xa66e
Unicode 0x31 0x32 0x33 0x4f60 0x597d
UTF-8 encoded 0x31 0x32 0x33 0xe4bda0 0xe5a5bd

And our hexdump’s here, note the byte order:

My test text files are here. More info:

Leave a Reply

Your email address will not be published. Required fields are marked *