How can I change a file's encoding with vim?

来源:互联网 发布:北京网络公安报警电话 编辑:程序博客网 时间:2024/06/07 07:40

I'm used to using vim to modify a file's line endings:

$ file filefile: ASCII text, with CRLF line terminators$ vim file:set ff=mac:wq$ file filefile: ASCII text, with CR line terminators

Is it possible to use a similar process to change a file's unicode encoding? I'm trying the following, which doesn't work:

$ file file.xmlfile.xml: Unicode text, UTF-16, little-endian$ vim file:set encoding=utf-8:wq$ file file.xmlfile.xml: Unicode text, UTF-16, little-endian

I saw someone say that he could "set fileencoding=utf-8, then update and write the file, and it works," but I seem to be missing something, or else he was confused. I don't know what he meant by "then update."


---------------------


From the doc:

:write ++enc=utf-8 russian.txt

So you should be able to change the encoding as part of the write command.

----------------------------------------------------

While using vim to do it is perfectly possible, why don't you simply use iconv? I mean - loading text editor just to do encoding conversion seems like using too big hammer for too small nail.

Just:

iconv -f utf-16 -t utf-8 file.xml > file.utf8.xml


---------------------------------

Just like your steps, setting fileencoding should work. However, I'd like to add one "set bomb" to help editor consider the file as UTF8.

$ vim file:set bomb:set fileencoding=utf-8:wq


reference 
http://stackoverflow.com/questions/778069/how-can-i-change-a-files-encoding-with-vim


0 0
原创粉丝点击