Wednesday 13 May 2020

How to convert text from one charset to another within vim

Sometimes when you edit a file with vim it sees the extended characters and guesses the charset of the file. If it isn't UTF-8 it usually guesses Latin-1 or ISO8859 and displays (converted) on the message line. In the case of a file of mine that had Chinese characters encoded in GBK, when I tried to convert the contents in situ with:

:1,$! iconv -f GBK -t UTF-8

I ended up with a buffer displaying the Chinese characters correctly, but when trying to write it out, vim indicated an error. Trying to force the write with w! gave me the original file.

The trick is you have to tell vim that the charset has changed. Do:

:set fileencoding=utf-8

before you write and quit and all will be well. You can check the current encoding at any point with:

:set fileencoding

No comments:

Post a Comment