如何在Vim中将cp1250特定字符替换为utf-8

时间:2011-03-31 15:46:38

标签: vim

我在Vim的cp1250编码中有一些中欧字符。当我使用 set encoding = utf-8 更改编码时,它们显示为<d0>等。如何在整个文件中替换这些字符应该是什么,即Đ,在这种情况下?

3 个答案:

答案 0 :(得分:3)

正如sidyll所说,你应该为此目的使用iconv。 Iconv知道的东西。它知道所有毛茸茸的编码,模糊的代码点,片假名,非规范化,规范形式,构图,非间距字符和其他。

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

或更短

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

过滤整个缓冲区。如果你这样做

:he xxd

如果需要,您将获得如何自动编码缓冲区加载/保存的示例。

iconv -l会列出你接受/知道的所有编码(很多:我的系统中的1168个)。

快乐的黑客攻击!

答案 1 :(得分: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}

答案 2 :(得分:1)

您可以将encoding设置为文件编码的值,将termencoding设置为UTF-8。请参阅The vim mbyte documentation

相关问题