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

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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:

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

Method 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).
    ...
    ...


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments