sponsor Vim development Vim logo Vim Book Ad

basic Tip #245: Working with Unicode (platform-independent)

 tip karma   Rating 73/26, Viewed by 6053 

created:   May 10, 2002 12:21      complexity:   basic
author:   Tony Mechelynck      as of Vim:   6.0

Here are the main options you will want to set if you want to work with Unicode files in (g)vim (see at bottom what help tags to look for)

if has("multi_byte")
     set encoding=utf-8                       " how vim shall represent characters internally
     setglobal fileencoding=utf-8         " empty is also OK (defaults to same as 'encoding'). Or you may want to set one of the ucs encodings (which
                                                          " may use less disk space if you use only "alphabetic" scripts such as Latin, Greek, Cyrillic, Hebrew or Arabic, and
                                                          " not "ideographic" scripts like Chinese, Japanese or Korean. With the ucs encodings it is usually better
     set bomb                                      "  to also set 'bomb' on ('byte-order-mark" option, irrelevant for utf-8 but not for ucs)
     set termencoding=iso-8859-15    " or whatever is appropriate to your locale (iso-8859-15 is Latin1 + Euro currency sign)
     set fileencodings=ucs-bom,iso-8859-15,iso-8859-3,utf-8
         " or whatever is appropriate to the kinds of files you want to edit
         " 'fileencodings' defines the heuristic to set 'fillencoding' (local to buffer) when reading an existing file. The first one that matches will be used.
         " ucs-bom is "ucs with byte-order-mark"; it must not come after ucs-8 if you want it to be used
     echoerr "Sorry, this version of (g)vim was not compiled with +multi_byte"

In "replace" mode, one utf character (one or more data bytes) replaces one utf character (which need not use the same number of bytes)
In "normal" mode, ga shows the character under the cursor as text, decimal, octal and hex; g8 shows which byte(s) is/are used to represent it
In "insert" or "replace" mode,
  - any character defined on your keyboard can be entered the usual way (even with dead keys if you have them, e.g. âêîôû  äëïöü)
  - any character which has a "digraph" (there are a huge lot of them, see :dig after setting enc=utf-8) can be entered with a Ctrl-K prefix
  - any utf character at all can be entered with a Ctrl-V prefix, either <Ctrl-V> u aaaa or <Ctrl-V> U bbbbbbbb, with 0 <= aaaa <= FFFF, or 0 <= bbbbbbbb <= 7FFFFFFF

Unicode can be used to create html "body text", at least for Netscape 6 and probably for IE; but on my machine it doesn't display properly as "title text" (i.e., between <title></title> tags in the <head> part).

Gvim will display it properly if you have the fonts for it, provided that you set 'guifont' to some fixed-width font which has the glyphs you want to use (Courier New is OK for French, German, Greek, Russian and more, but I'm not sure about Hebrew or Arabic; its glyphs are of a more "fixed" width than those of, e.g. Lucida Console: the latter can be annoying if you need bold Cyrillic writing).


:h utf8
:h 'enc'
:h 'fenc'
:h 'fencs'
:h 'tenc'
:h 'bomb'
:h 'guifont'
:h ga
:h g8
:h i_Ctrl-V_digit

Happy Vimming !

 rate this tip  Life Changing Helpful Unfulfilling 

<<Ask vim where an option was set. | Working with Unicode (the same, rewritten for legibility) >>

Additional Notes

Anonymous, November 17, 2003 21:37
Also see here:
Anonymous, March 31, 2004 0:48
See also:

:h ++enc
Anonymous, March 7, 2006 8:23
I've basically given up reading a Unicode wide character file created under WinXP when I came
across this last tip, posted above.

In gvim:

:e ++enc=ucs-2le filename
James.Moritz@hummingbird.com, August 10, 2006 7:17
Thanks very much for the tip! I just wanted to point out that the byte order mark is always relevent for Unicode text files, even UTF-8. The BOM does more than just specify byte ordering, it also indicates what the encoding is. Without it, a UTF-8 file would be mistaken for ASCII.
nbmm74g@freemail.com, December 26, 2006 13:53
dynamites immodest hideouts cowerers oriented coal!incredulity pineapple apocalyptic noiselessly,flyers http://casinosenespanol.casinoqz.com/##-casinos-en-espaol- reap,gabled beautified http://apostandocasino.casinolc.com/##-apostando-casino- officious Jericho.reaped sleeplessly http://chancesdeganarplataenlinea.casinolc.com/##-machine-slot-chances-de-ganar-plata-en-lnea summed hurried http://elcasinodelasuerte.casinoot.com/##-el-casino-de-la-suerte-download-opponents-video-strip-poker- bereavements unsatisfied http://lasvegascasinoenlinea.dcasinoa.com/##-las-vegas-casino-en-lnea- subdirectory unsophisticated hampers microphone,http://mejorescasinosenespanol.zccasino.com/##-mejores-casinos-en-espaol- anachronism significance audiology bonanza debaters http://nuevoscasinosenlinea.casinooc.com/##-nuevos-casinos-en-lnea-jugar-online-tragaperras-gratis- mutating Scot!vomited suds http://sitiosdecasinoenlinea.casinoqz.com/##-sitios-de-casino-en-lnea- Morris specimen!furthest lacerate?suffix:http://rouletteenlinea.casinoot.com/##-roulette-en-linea- synchrony!adjusting?http://slotsmachinesenlinea.dcasinoq.com/##-slots-machines-en-linea- - Tons of interesdting stuff!!!
tw@hotmail.com, December 28, 2006 16:16
draining widest methods clothed askew rusted Paleozoic?http://berthite.gws.uky.edu/bb2/Members/kelly/online-phentermine-diet-pills.html##-online-phentermine-diet-pills- nanostores intermediate http://berthite.gws.uky.edu/bb2/Members/kelly/buy-cheap-phentermine.html##-buy-cheap-phentermine- Moines transcriptions helix http://berthite.gws.uky.edu/bb2/Members/kelly/phentermine-online.html##-phentermine-online- kidding!extravagance glimmer http://berthite.gws.uky.edu/bb2/Members/kelly/buy-phentermine.html##-buy-phentermine- troubleshooting.plasters http://berthite.gws.uky.edu/bb2/Members/kelly/cheap-phentermine.html##-cheap-phentermine- election excellences Cavendish http://berthite.gws.uky.edu/bb2/Members/kelly/buy-phentermine-online.html##-buy-phentermine-online- tills tape,http://berthite.gws.uky.edu/bb2/Members/kelly/order-phentermine.html##-order-phentermine- Intelsat scaffolding mysterious http://berthite.gws.uky.edu/bb2/Members/kelly/online-phentermine.html##-online-phentermine- gentlest descriptor milkers burglarproofs superstition.http://berthite.gws.uky.edu/bb2/Members/kelly/cheap-phentermine-online.html##-cheap-phentermine-online- Argentina stirs billers trier http://berthite.gws.uky.edu/bb2/Members/kelly/phentermine-prescription.html##-phentermine-prescription- ... Thanks!!!
8xcee6@mail.nu, December 30, 2006 14:53
counterpoise,hitherto enema Mildred:greenhouse annuals shadowed abhorred,prohibitory http://berthite.gws.uky.edu/bb2/Members/kelly/online-phentermine-diet-pills.html##-online-phentermine-diet-pills- dryly confuser undertakers http://berthite.gws.uky.edu/bb2/Members/kelly/buy-cheap-phentermine.html##-buy-cheap-phentermine- phases.outlaw separator http://berthite.gws.uky.edu/bb2/Members/kelly/phentermine-online.html##-phentermine-online- electron!heater frontage?miniaturizing http://berthite.gws.uky.edu/bb2/Members/kelly/buy-phentermine.html##-buy-phentermine- instigates,solidarity stratify?cornea.followed!http://berthite.gws.uky.edu/bb2/Members/kelly/cheap-phentermine.html##-cheap-phentermine- meet experimentally http://berthite.gws.uky.edu/bb2/Members/kelly/buy-phentermine-online.html##-buy-phentermine-online- gotten compactors!bode hygiene http://berthite.gws.uky.edu/bb2/Members/kelly/order-phentermine.html##-order-phentermine- ANSI swished http://berthite.gws.uky.edu/bb2/Members/kelly/online-phentermine.html##-online-phentermine- parliamentary,port http://berthite.gws.uky.edu/bb2/Members/kelly/cheap-phentermine-online.html##-cheap-phentermine-online- Venusian sublimations.momentariness dreariness declarative http://berthite.gws.uky.edu/bb2/Members/kelly/phentermine-prescription.html##-phentermine-prescription- adjoining
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to vim@vim.org after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge.net Logo