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?

up vote 15 down vote accepted

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

  • 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. – pascal May 19 '15 at 1:44
  • 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 – Philip Kearns Feb 14 at 15:01

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).
    ...
    ...
  • 3
    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>. – Totor Jan 25 '14 at 1:41

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.