124

I work on webpages involving non-English scripts from time to time, most of them are encoded using UTF-8.

Vim and gVim do not display those UTF-8 characters correctly.

I'm using Vim 7.3.46 on Windows 7, 64-bit, with set guifont=Monaco:h10 in _vimrc.

Is there a way to fix this?

Update: I've googled around and found set guifontwide acts as second fallback for regional languages.

I added the following lines to _vimrc and most of my problems got solved.

set enc=utf-8
set fileencoding=utf-8
set fileencodings=ucs-bom,utf8,prc
set guifont=Monaco:h11
set guifontwide=NSimsun:h12

The above NSimsun font works for Chinese, The problem is, I don't know how they got the font name to work with Vim, Courier New is mentioned as Courier_New also NSimsun is nowhere in the font directory. The font I want to use is Latha But, I don't know how to use it in the _vimrc file. set guifontwide=latha:h12 or set guifontwide=Latha:h12 doesn't work.

If I successfully set the guifontwide to latha, then my problem will be solved. How to do it?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Sathish Manohar
  • 5,859
  • 10
  • 37
  • 47
  • what is the value of `'guifont'` and the value of `'guifontwide'`? Also, what OS are you using and what version of vim? – Benoit Mar 02 '11 at 12:19
  • Sathish, were you able to find any solution for this? I'm trying to type in gVim with eKalappai software but vim is only showing me ?? characters (apparently saving them the same way). – Sundar R Aug 26 '13 at 12:52
  • @sundar No. I gave up :| – Sathish Manohar Aug 29 '13 at 08:14
  • My problem was that inconsolata-g does not support the utf-8 characters in my document. – AndrewPK Oct 17 '13 at 15:12
  • Using `set guifontwide=NSimsun:h12` fails for me, says "Invalid wide font" probably the same non-monospace issue? – mikew Mar 04 '15 at 14:12

9 Answers9

72

Did you try

:set encoding=utf-8
:set fileencoding=utf-8

?

Benoit
  • 76,634
  • 23
  • 210
  • 236
Maxim Sloyko
  • 15,176
  • 9
  • 43
  • 49
  • 6
    I've tried with both these settings, and still I cant see utf-8 characters. – Sathish Manohar Mar 02 '11 at 13:38
  • 1
    -1. Actually this cannot work. Neither of these options will reload the file. The first one will modify how vim stores buffers (and other things) internally, the second one will specify that you wish to change the encoding of a file that is already loaded in a buffer, which will apply when saving it. – Benoit Mar 03 '11 at 15:51
  • `:set encoding=utf8` *WILL* refresh the buffer and if the problem was in incorrect encoding, it will fix it. I added `set fileencoding=utf8` option so you won't run into the most confusing "Can't convert some character" error when saving. – Maxim Sloyko Mar 04 '11 at 12:21
  • set encoding worked. I added set encoding=utf8 to .vimrc file and now everything is ok with utf8. – Alexander Kim Jun 22 '16 at 04:50
  • On OSX, I've added these two lines (without the colons) to my `~/.vimrc` and it worked fine – lionello Nov 10 '18 at 01:56
  • Thanks! That did it for me. – U. W. Oct 23 '19 at 07:14
  • This did work for me for cyrillic after following this [answer](https://superuser.com/questions/317661/strange-vim-behaviour) and installing iconv library libiconv on Windows 10. Still no luck with Japanese. – queezz Jan 23 '20 at 12:00
71

Try to reload the document using:

:e! ++enc=utf8

If that works you should maybe change the fileencodings settings in your .vimrc.

Benoit
  • 76,634
  • 23
  • 210
  • 236
  • 2
    Doesn't change anything for me – Thomas Levesque Sep 01 '14 at 12:41
  • 9
    **DO NOT** run this before saving your work as it will wipe the changes you have made. `:u` will save you though. –  May 20 '15 at 18:30
  • Also this sets the file to readonly when you do this, so you also need to then run `:set noro` – Matt Vukomanovic Oct 11 '16 at 01:54
  • 2
    Thanks for this. Vim was not rendering utf-8 in my case because I had a block of binary data (multipart/form-data attachment part). When I did `:e! ++enc=utf8`, it did render the utf-8, but complained `[ILLEGAL BYTE in line 286]` (the first binary part line) inside the file. I'm guessing the default behavior is to fall back silently to some other encoding when this happens on Vim opening. For some reason, though, `echo &enc` still reports utf-8 in that case. – Dmitry Minkovsky Jan 17 '17 at 02:28
  • 1
    @DmitryMinkovsky, `&enc` is the encoding vim uses internally to store buffer contents. The setting that contains the encoding for the current file is `&fenc` – Benoit Jan 17 '17 at 09:38
  • This worked for me when other things didn't, thanks @Benoit – Grant Bowman May 10 '18 at 03:06
13

If Japanese people come here, please add the following lines to your ~/.vimrc

set encoding=utf-8
set fileencodings=iso-2022-jp,euc-jp,sjis,utf-8
set fileformats=unix,dos,mac
kujiy
  • 5,833
  • 1
  • 29
  • 35
  • See also [this answer](https://stackoverflow.com/questions/39840843/how-to-display-utf-8-characters-in-vim-correctly) regarding Japanese characters. – Matthias Braun Jun 16 '22 at 08:22
6

On Microsoft Windows, gvim wouldn't allow you to select non-monospaced fonts. Unfortunately Latha is a non-monospaced font.

There is a hack way to make it happen: Using FontForge (you can download Windows binary from http://www.geocities.jp/meir000/fontforge/) to edit the Latha.ttf and mark it as a monospaced font. Doing like this:

  1. Load fontforge, select latha.ttf.
  2. Menu: Element -> Font Info
  3. Select "OS/2" from left-hand list on Font Info dialog
  4. Select "Panose" tab
  5. Set Proportion = Monospaced
  6. Save new TTF version of this font, try it out!

Good luck!

Community
  • 1
  • 1
Yi Zhao
  • 6,768
  • 1
  • 18
  • 18
  • 1
    I could not change latha font to monospace. I followed your steps exactly but the saved file still doesn't work as monospaced font :( – Sathish Manohar Apr 25 '11 at 08:43
  • 1
    @Sathish Manohar 1) overwrite the existed TTF file, restart your Windows, and try it. or 2) you can save the TTF to a new file and modify the font name. Install the new TTF file. This new font should be available in your list. – Yi Zhao Apr 25 '11 at 10:57
  • After all those years (7 years) later, there are several free fonts available for VIM. Google Noto fonts https://www.google.com/get/noto; and Iosveka https://github.com/be5invis/Iosevka/releases are very good alternatives to evaluate. – Yi Zhao Dec 17 '18 at 05:37
5

In Linux, Open the VIM configuration file

$ sudo -H gedit /etc/vim/vimrc

Added following lines:

set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8

Save and exit, and terminal command:

$ source /etc/vim/vimrc

At this time VIM will correctly display Chinese.

5

this work for me and do not need change any config file

vim --cmd "set encoding=utf8" --cmd "set fileencoding=utf8" fileToOpen

user2204107
  • 111
  • 1
  • 4
2

Is this problem solved meanwhile?

I had the problem that gvim didn't display all unicode characters (but only a subset, including the umlauts and accented characters), while :set guifont? was empty; see my question. After reading here, setting the guifont to a sensible value fixed it for me. However, I don't need characters beyond 2 bytes.

Community
  • 1
  • 1
Tobias
  • 2,481
  • 3
  • 26
  • 38
2

I couldn't get any other fonts I installed to show up in my Windows GVim editor, so I just switched to Lucida Console which has at least somewhat better UTF-8 support. Add this to the end of your _vimrc:

" For making everything utf-8
set enc=utf-8
set guifont=Lucida_Console:h9:cANSI
set guifontwide=Lucida_Console:h12

Now I see at least some UTF-8 characters.

Ryan Shillington
  • 23,006
  • 14
  • 93
  • 108
  • in my case cANSI had to be changed to cGREEK (Edit -> Select Font... in GVim), with Consolas font, in order to display a UTF-8 encoded text file with greek characters correctly – hello_earth Aug 06 '18 at 07:47
1

On Windows gvim just select "Lucida Console" font.

Alex Dvoretskiy
  • 317
  • 2
  • 9