Showing Invisible Things In Vim

Showing normal invisible characters

Sometimes you want to see invisible things. Imagine you’re programming in Python, where it really matters if something is indented with spaces or tabs and the number used matters too.

First we turn on list. Sadly, I can’t tell you why. The help docs are all about using lists in VimScript and not what this command does. What I can tell you is that this stuff doesn’t work without it. ;)

set list

Next we tell vim what we want it to display in place of those invisible characters. The arguments to lcs (lscope) take the form of <character to show>:<what to display for it> in a comma separated list. Here’s what I use.

set lcs=tab:»_,trail:·

That says show »_ whenever there’s a tab, and · whenever there’s an invisible character trailing at the end of the line. You don’t have to use fancy characters like that. You could use >_ and - if you wanted, or anything else. I’d avoid using . (period) for trail because you might confuse it for an actual period.

Here’s an example of that command. I’ve highlighted the invisible characters in orange to make them easier to see, but I’d recommend something more muted so they’re not always shouting at you. See below.

an example of the lcs command without the eol option

I’ve currently got my Vim set to have tabs be four characters wide. This means there’s a tabstob every 4 characters. That’s why the tab between “foo” and “bar” is only one character wide. It also shows that Vim is smart enough to only show as much of the thing as is necessary, and it’ll repeat the 2nd character of the tab display to fill the width needed.

You can also tell it to specifically display a character for the end of the line. If you wanted it to display a dollar sign wherever there is a newline character in your document you would add eol:$ to that.

set lcs=tab:»_,trail:·,eol:$

an example of the lcs command without the eol option

Next we’re going to improve the looks of this. We don’t want lots of characters that are the same color as the stuff we’re actually writing. It just gets confusing.

I like to have mine dim, and I’ve got a dark gray background so I say

highlight SpecialKey ctermfg=8 guifg=DimGrey

If you wanted to make them stand out you might make them yellow with

highlight SpecialKey ctermfg=14 guifg=Yellow

Those lines tell Vim “highlight the SpecialKey characters with these colors…” ctermfg sets the foreground color in the terminal and guifg sets the foreground color in a graphical client (which has more options).

See :help ctermfg and :help guifg for more about specifying colors.

Showing Levels of Indentation

Some editors have the ability to draw a vertical line at each level of indentation. Many folks find this helpful when formatting their code.

While Vim doesn’t have any built-in support for this, there is the wonderful indentLine plugin. Here’s an example of it with some Perl 5 code.

example screenshot of indentline plugin

You can customize the character used, and its color. You can even make each indent level have a different character.

Shedding light on other weird characters

Sometimes you get a file with a character that’s just displaying weirdly, or not displaying at all, but you can see that something is there.

In normal mode you can move your cursor over it and type ga or :ascii That will

Print the ascii value of the character under the cursor in decimal, hexadecimal and octal. Mnemonic: Get Ascii value.

Note that there’s no : before the ga. You just type it directly in normal mode.

Here are two examples. That weird chevron character I’m using to denote the start of a tab above

<»> 187, Hex 00bb, Oct 273, Digr >>

and the dot in the middle of the line.

<·> 183, Hex 00b7, Oct 267, Digr .M

Looking up those hex codes in Google tels me they’re “» open (or close) chevron-quotes” and Unicode Character ‘MIDDLE DOT’ (U+00B7). Exactly correct.