Changing file encoding with Vim
I found a web page that showed a few funky characters. It must be an encoding problem so I thought about how to fix this. This is how I did.
First I wanted to know how the template file was encoded. To do that I opened Vim and ran “:set fileencoding”. Vim displayed “latin1″. To make the file utf-8 I ran “:set fileencoding=utf-8″ and saved it. The web page looked ok after that.
To verify the encoding one can use a hex editor. I used khexedit (KDE program) to see what my funny character was in hex. Before I changed the encoding the character was E5 in hex. Python can verify that å should be E5:
>>> u’å’.encode(’latin1′)
‘\xe5′
So the encoding is latin1. To verify that vim changed the file correctly one can check with khexedit after changing the file. khexedit now showed C3 A5 for this character. Checking with python again:
>>> u’å’.encode(’utf-8′)
‘\xc3\xa5′
C3 A5 seems to be correct hex for the å character in utf-8 encoding, so know I can be certain Vim did the right thing.
Thanks for this post.
As an “old” vimer I was never sure if vim is storing my files correctly, now I know.
jukart
Thanks!
I am developing a website for my final project at trade school, and the files were all messy, some latin1, some utf-8.
Now I know how to transform them!