Fix: Garbled Text in Vim on Windows, Settings to Support UTF-8 Encoding

After installing Vim on Windows, files opened with garbled text. Many methods found online are settings for GBK encoding, but I had no problem opening GBK-encoded files in the first place. What actually needed fixing was garbled display in UTF-8 files.

Solution 1

This method worked for me.

Add the following two lines to the very beginning of C:Program FilesVim_vimrc. If the file does not exist, create a new _vimrc file.

let &termencoding=&encoding
set fileencodings=utf-8,gbk,ucs-bom,cp936

After saving, open the files with Vim again and check. UTF-8 and GB2312 should both work without problems.

Solution 2

This solution failed for me.

Simplified Chinese

If you are using Vim on Simplified Chinese Windows and want to edit UTF-8 files with Vim, you can set the following options in Vim's configuration file. After a default Vim 7.2 installation, the configuration file is usually the _vimrc file in the Vim directory. Open it with Notepad or another editor to edit it.

set encoding=utf-8
set termencoding=gb2312
set fileencodings=ucs-bom,utf-8,chinese
if has("win32")
  set fileencoding=chinese
else
  set fileencoding=utf8
endif
set ambiwidth=double
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
language messages zh_CN.UTF-8

Just add this content to the very beginning of the _vimrc file.

Traditional Chinese

Below is the configuration for Traditional Chinese systems:

set encoding=utf-8
set termencoding=big5
set fileencodings=ucs-bom,utf-8,chinese
if has("win32")
  set fileencoding=chinese
else
  set fileencoding=utf8
endif
set ambiwidth=double
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
language messages zh_TW.UTF-8

Leave a Reply