2011-03-31 59 views
1

我在Vim中使用了cp1250編碼中的中歐字符。當我用set encoding = utf-8更改編碼時,它們看起來像<d0>等。在這種情況下,我怎樣才能在整個文件中替換這些字符,即它們應該是什麼?如何在Vim中將cp1250特定字符替換爲utf-8

回答

2

iconv()功能可能是有用的:

iconv({expr}, {from}, {to})    *iconv()* 
     The result is a String, which is the text {expr} converted 
     from encoding {from} to encoding {to}. 
     When the conversion fails an empty string is returned. 
     The encoding names are whatever the iconv() library function 
     can accept, see ":!man 3 iconv". 
     Most conversions require Vim to be compiled with the |+iconv| 
     feature. Otherwise only UTF-8 to latin1 conversion and back 
     can be done. 
     This can be used to display messages with special characters, 
     no matter what 'encoding' is set to. Write the message in 
     UTF-8 and use: 
      echo iconv(utf8_str, "utf-8", &enc) 
     Note that Vim uses UTF-8 for all Unicode encodings, conversion 
     from/to UCS-2 is automatically changed to use UTF-8. You 
     cannot use UCS-2 in a string anyway, because of the NUL bytes. 
     {only available when compiled with the +multi_byte feature} 
+0

或者你可以使用iconv外部命令 – sehe 2011-03-31 22:17:18

+0

@sehe:啊,那好多了!特別是如果需要轉換整個緩衝區。 @Rook,檢查一下,你只需要一點Vim過濾器('!')魔法。 @sehe,爲什麼不把它作爲答案發布,以便我們能夠投票? – sidyll 2011-04-01 01:57:02

+0

說實話,我喜歡調用過濾器程序的'簡單',但我喜歡使用內建函數的建議!我不知道它在那裏:) – sehe 2011-04-01 06:30:15

3

正如sidyll說,你確實應該使用的iconv爲宗旨。 Iconv知道的東西。它知道所有的毛茸茸的編碼,固定的代碼點,片假名,非規範化,規範形式,組合,非間距字符和其他。

:%!iconv --from-code cp1250 --to-code utf-8 

或更短

:%!iconv -f cp1250 -t utf-8 

過濾整個緩衝區。如果你這樣做

:he xxd 

你會得到一個如何自動編碼緩存加載/保存如果你想要的樣本。

iconv -l將列出您接受/瞭解的所有(很多:1168在我的系統上)編碼。

快樂黑客!