Thursday, 22 January 2009

vim converting between MS Unicode UCS-2LE and UTF-8

Many times at work I need to convert between 2 different encodings from Unicode:
1. MS Unicode -> generated e.g. by Excel when Saving data as Unicode text
2. UTF8 encoding (standard)

Requirement VIM is compiled with multi_byte support.
check if in VIM normal mode
:echo has('multi_byte')

returns 1

In VIM normal mode to open a file with certain encoding:
:e ++enc=<encoding> <filename>

To save in different encoding:
:w ++enc=<encoding> <filename>

e.g.
open in ucs-2le -> save in utf-8
:e ++enc=ucs-2le /tmp/file_ucs2le.csv
:w ++enc=utf-8 /tmp/file_utf8.csv

It works even if your fileencoding in your .vimrc (_vimrc on Windows) is different and VIM normally would recognize as different encoding from the one you want.

++enc sets fileencoding and not encoding (filencoding is needed when opening or saving the file)

More on Unicode in VIM under
http://vim.wikia.com/wiki/Working_with_Unicode

2 comments:

Clover said...

I’m very glad to found this website because; it carries awesome and actually good data in favor of readers.

clover
www.n8fan.net

Leslie Lim said...

Pretty good post. I found your website perfect for my needs. Thanks for sharing the great ideas.


Chix
www.imarksweb.org