vim只能显示ASCII字符,并将其他字节视为二进制数据吗?


13

我已经知道vim -b,但是,根据所使用的语言环境,它会将多字节字符(如UTF-8)显示为单个字母。

我怎么能要求vim只显示ASCII可打印字符,并将其余字符视为二进制数据,而不考虑字符集?

Answers:


18

使用时vim -b,这会将所有高位字符显示为<xx>

set encoding=latin1
set isprint=
set display+=uhex

任何单字节编码都可以使用,vim对所有低位字符使用ASCII并将其硬编码为可打印。设置isprint为空将把其他所有内容标记为不可打印。设置uhex会将它们显示为十六进制。

以下是每个命令后显示的变化:

之前 设置编码后 设置isprint之后 设置uhex后


这些选项不依赖-b,它们只会设置其他几个选项,请参见:help edit-binary。我看不出不可打印字节的显示方式有所不同(它-b通常不显示NUL )。我主要不使用-b,因为我使用这些选项来检查文本文件中的奇怪编码。
帕斯卡

从Word粘贴到文本中时,我将非常方便使用。这些命令也可以放在一行上:set encoding=latin1|set isprint=|set display+=uhex
Philip Kearns,

1

这听起来像您要找的东西。vimWiki上的技巧提示为:强制UTF-8 Vim将Latin1读取为Latin1

$ vim -c "e ++enc=latin1" file.txt

同样来自vim:help,你可以做到这一点,看看更多的编码。

:help enc

摘录自 :help enc

'encoding' 'enc'        string (default: "latin1" or value from $LANG)
                        global
                        {only available when compiled with the +multi_byte
                        feature}
                        {not in Vi}
    Sets the character encoding used inside Vim.  It applies to text in
    the buffers, registers, Strings in expressions, text stored in the
    viminfo file, etc.  It sets the kind of characters which Vim can work
    with.  See encoding-names for the possible values.

    NOTE: Changing this option will not change the encoding of the
    existing text in Vim.  It may cause non-ASCII text to become invalid.
    It should normally be kept at its default value, or set when Vim
    starts up.  See multibyte.  To reload the menus see :menutrans.

    This option cannot be set from a modeline.  It would most likely
    corrupt the text.

    NOTE: For GTK+ 2 it is highly recommended to set 'encoding' to
    "utf-8".  Although care has been taken to allow different values of
    'encoding', "utf-8" is the natural choice for the environment and
    avoids unnecessary conversion overhead.  "utf-8" has not been made
    the default to prevent different behavior of the GUI and terminal
    versions, and to avoid changing the encoding of newly created files
    without your knowledge (in case 'fileencodings' is empty).
    ...
    ...

3
很好,但是我希望“ vim仅显示ASCII可打印字符”,并且您的解决方案使用latin1字符集(即ISO-8859-1,ASCII的超集),因此它将显示诸如é“ d宁愿显示为<e9>
Totor
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.