1

Vim shows non-printable characters prefixed with a ^ (for instance ^@ for a NUL byte). I have a column based file containing both printable and non-printable characters which is difficult to read, since each non-printable character shifts all remaining columns one character to the right.

Is there a way to hide non-printable characters or simply display a placeholder char instead? I also don't mind having every character be represented by two characters.

3 Answers 3

7

This is controlled by the 'isprint' option. Since (quoting from the :help) "The characters from space (ASCII 32) to '~' (ASCII 126) are always displayed directly", the only way is to condense the special characters. You can do that via

:set isprint=1-255

Depending on your font, those characters (like ^[) will probably show up as empty display cells or a generic replacement character.

7
  • Thanks. This works 99%. I cannot figure out how to hide/display the NUL byte
    – knittl
    Jun 10, 2014 at 10:39
  • Me neither; the option doesn't accept the value 0; I think that's a bug. Jun 10, 2014 at 10:45
  • One thing I also noticed, control characters are now intpreted by the shell as well (which can mess up the display). Any way around that?
    – knittl
    Jun 10, 2014 at 10:59
  • What exactly do you mean by that?! Jun 10, 2014 at 11:45
  • I've just posted the issue with the 0 on vim_dev: article.gmane.org/gmane.editors.vim.devel/46024 Jun 10, 2014 at 15:27
3

Maybe you could use the conceal feature:

:syn match nonprinting /[^[:print:]]/ conceal cchar=%

And set conceallevel if you haven't already:

:set conceallevel=1

This will still expand out the characters when the cursor is on them:

bunch of nul characters same shown as <code>%</code> characters

0

to better understand @muru' s answer:

Maybe you could use the conceal feature:

:syn match nonprinting /[^[:print:]]/ conceal cchar=%
:set conceallevel=1

Another example

:syn match name_you_like    /[^[:print:]]/ conceal cchar=!
:set conceallevel=2

after this following line, set concealcursor? gets concealcursor=

:set concealcursor='nvic'

And I don't know why.

update: we can do

:set concealcursor=nvic

or

:set concealcursor=nc

Just play around with those 3 settings.


explaination:

Each part Description
syn match defines one match
nonprinting a group-name, whatever you like?
/[^[:print:]]/ a pattern
  • [:print:]
  • ^
  • [ ]
  • / /
  • printable characters, including space
  • regex usage, to exclude
  • regex usage, any of inside
  • Can use *something* "something" instead. In the syntax commands, a pattern must be surrounded by two identical characters. This is like it works for the ":s" command. The most common to use is the double quote
conceal mark the items you specify as concealable
cchar=% conceal character, here it is %

Details from doc

1. syn match

Maybe we can ignore those syntac elements which didn' t appear above...

DEFINING MATCHES                    *:syn-match*

:sy[ntax] match {group-name} [{options}]
        [excludenl]
        [keepend]
        {pattern}
        [{options}]


    {group-name}        A syntax group name such as "Comment".
    [{options}]     See |:syn-arguments| below.
    [excludenl]     Don't make a pattern with the end-of-line "$"
                extend a containing match or region.  Must be
                given before the pattern. |:syn-excludenl|
    keepend         Don't allow contained matches to go past a
                match with the end pattern.  See
                |:syn-keepend|.
    {pattern}       The search pattern that defines the match.

2. :syn-pattern

Syntax patterns                              :syn-pattern E401 E402

In the syntax commands, a pattern must be surrounded by two identical
characters.  This is like it works for the ":s" command.  The most common to
use is the double quote.  But if the pattern contains a double quote, you can
use another character that is not used in the pattern.  Examples:
  :syntax region Comment  start="/\*"  end="\*/"
  :syntax region String   start=+"+    end=+"+   skip=+\\"+

See pattern for the explanation of what a pattern is.  Syntax patterns are
always interpreted like the 'magic' option is set, no matter what the actual
value of 'magic' is.

And the patterns are interpreted like the 'l' flag is  not included in 'cpoptions'.
This was done to make syntax files portable and  independent of the 'magic' setting.

Try to avoid patterns that can match an empty string, such as "[a-z]*".
This slows down the highlighting a lot, because it matches everywhere.
[:alnum:]         [:alnum:]   isalnum   ASCII letters and digits
[:alpha:]         [:alpha:]   isalpha   ASCII letters
[:blank:]         [:blank:]             space and tab
[:cntrl:]         [:cntrl:]   iscntrl   ASCII control characters
[:digit:]         [:digit:]             decimal digits '0' to '9'
[:graph:]         [:graph:]   isgraph   ASCII printable characters excluding
                                        space
[:lower:]         [:lower:]   (1)       lowercase letters (all letters when
                                        'ignorecase' is used)
[:print:]         [:print:]   (2)       printable characters including space
[:punct:]         [:punct:]   ispunct   ASCII punctuation characters
[:space:]         [:space:]             whitespace characters: space, tab, CR,
                                        NL, vertical tab, form feed
[:upper:]         [:upper:]   (3)       uppercase letters (all letters when
                                        'ignorecase' is used)
[:xdigit:]        [:xdigit:]            hexadecimal digits: 0-9, a-f, A-F
[:return:]        [:return:]            the <CR> character
[:tab:]           [:tab:]               the <Tab> character
[:escape:]        [:escape:]            the <Esc> character
[:backspace:]     [:backspace:]         the <BS> character
[:ident:]         [:ident:]             identifier character (same as "\i")
[:keyword:]       [:keyword:]           keyword character (same as "\k")
[:fname:]         [:fname:]             file name character (same as "\f")
          The square brackets in character class expressions are additional to
          the square brackets delimiting a collection.

          For example, the  following is a plausible pattern for a UNIX filename:

          [-./[:alnum:]_~]\+`

          That is, a list of at least one character,  each of which is
          either '-', '.', '/', alphabetic, numeric, '_' or  '~'.

3. conceal

conceal                                         conceal :syn-conceal

When the "conceal" argument is given, the item is marked as concealable.
Whether or not it is actually concealed depends on the value of the
'conceallevel' option.

4. cchar

cchar                                                   :syn-cchar
                                                        E844
The "cchar" argument defines the character shown in place of the item
when it is concealed  If "cchar" is not set then the default conceal
character defined in the 'listchars' option is used.  The character cannot be
a control character such as Tab.
See hl-Conceal for highlighting.

5. conceallevel


                                                'conceallevel' 'cole'
'conceallevel' 'cole'   number (default 0)
                        local to window
        Determine how text with the "conceal" syntax attribute :syn-conceal
        is shown:

        Value           Effect
        0               Text is shown normally
        1               Each block of concealed text is replaced with one
                        character.  If the syntax item does not have a custom
                        replacement character defined (see :syn-cchar) the
                        character defined in 'listchars' is used.
                        It is highlighted with the "Conceal" highlight group.
        2               Concealed text is completely hidden unless it has a
                        custom replacement character defined (see
                        :syn-cchar).
        3               Concealed text is completely hidden.

        Note: in the cursor line concealed text is not hidden, so that you can
        edit and copy the text.  This can be changed with the 'concealcursor'
        option.

6. concealcursor

                                                'concealcursor' 'cocu'
'concealcursor' 'cocu'  string (default: "")
                        local to window
        Sets the modes in which text in the cursor line can also be concealed
        When the current mode is listed then concealing happens just like in
        other lines.
          n             Normal mode
          v             Visual mode
          i             Insert mode
          c             Command line editing, for 'incsearch'

        'v' applies to all lines in the Visual area, not only the cursor.
        A useful value is "nc".  This is used in help files.  So long as you
        are moving around text is concealed, but when starting to insert text
        or selecting a Visual area the concealed text is displayed, so that
        you can see what you are doing.
        Keep in mind that the cursor position is not always where it's
        displayed.  E.g., when moving vertically it may change column.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .