How to solve vim encoding problem when using vim to edit files ?

Problem

When you want to use vim to edit a text file, you would get this problem ( some unicode characters are not properly displayed):

vi a.txt

text='?????????1298?09?01?'

Solution

You should specify the vim encoding configurations in your environment.

Step #1: Create a vim configuration file in your home directory

cd ~
touch .vimrc

Step #2: Set the encoding properties in your ~/.vimrc file

Enter following configrations in your ~/.vimrc file:

set fileencodings=utf-8,gb2312,gb18030,gbk,ucs-bom,cp936,latin1
set enc=utf8
set fencs=utf8,gbk,gb2312,gb18030

Step #3: Exit your shell and re-login the server

Exit your shell and re-login the server like this:

exit

Test

Then you can test your vim again:

vi a.txt

text='正确09年01日'