Can vim display ASCII characters only, and treat other bytes as binary data?

16,396

Solution 1

When using vim -b, this displays all high characters as <xx>:

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

Any single-byte encoding will work, vim uses ASCII for all lower chars and has them hard-coded as printable. Setting isprint to empty will mark everything else as non-printable. Setting uhex will display them as hexadecimal.

Here is how the display changes after each command:

before after setting encoding after setting isprint after setting uhex

Solution 2

This sounds like what you're looking for. This tip from the vim wiki titled: Forcing UTF-8 Vim to read Latin1 as Latin1.

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

Also from vim's :help you can do this to see more on encodings.

:help enc

excerpt from :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).
    ...
    ...
Share:
16,396

Related videos on Youtube

Totor
Author by

Totor

Updated on September 18, 2022

Comments

  • Totor
    Totor over 1 year

    I already know vim -b, however, depending on the locale used, it displays multi-byte characters (like UTF-8) as single letters.

    How can I ask vim to only display ASCII printable characters, and treat the rest as binary data, no matter the charset?

  • Totor
    Totor over 10 years
    That's pretty nice, but I'd like "vim to only display ASCII printable characters", and your solution uses the latin1 charset (that is ISO-8859-1, a superset of ASCII), it will thus display characters such as é which I'd rather like to be displayed as <e9>.
  • pascal
    pascal almost 9 years
    These options don't depend on -b, that will just set a few other options, see :help edit-binary. I don't see a difference in how nonprintable bytes are shown (it shows NUL without -b as well usually). I'm mostly not using -b, because I use these options to check on weird encodings in text files.
  • user3691006
    user3691006 about 6 years
    Very handy, I'm going to use it when pasting from Word into text. Those commands can also be put onto a single line: set encoding=latin1|set isprint=|set display+=uhex