Skip to main content

Advanced Vim Concepts

Show Hidden Special Characters in Vim

Wondering if it is 2 spaces or tabs? You can show special characters in Vim and easily identify them.

Have you ever encountered a problem in a YAML, Python or any other files that emphasizes indentation?

And later, you realized that everything in the file was correct, except for the indentation and saw that there was a line indented with 3 spaces instead of 4 spaces?

That happens often. Most IDEs let you view the special characters and identify the end of line, spaces and tabs. Vim is not behind any other IDE. It can also show you those hidden characters.

Hidden characters in Vim

Hidden characters in Vim can be thought of as "whitespaces". Below are the characters that Vim considers hidden for better readability.

  • eol (end of line)
  • tab
  • trail (space character before a newline character)
  • extends (character in the last column to show that next line is continuation of line wrap)
  • precedes (character in the first column to show that this line is continuation of previous line as a line wrap)
  • conceal
  • nbsp (non breakable space character)

Enable visibility of hidden characters

To temporarily enable the visibility of hidden characters, you can use the following command.

:set list

To reverse this change, you can hide the hidden characters again by using the command given below.

:set nolist
enabling visibility of hidden characters

As you can see, enabling the list option, there is now $ character denoting a new-line or a line break.

To make this change permanent, add the following line to your 'vimrc' file.

set list

Change hidden characters

When you turn on visibility of characters, Vim has a predefined set. As you saw in the gif above, enabling visibility using set list, the newline character was denoted by the dollar sign $.

But what if you don't want the dollar sign? What if you want a different character to denote a line break?

Below is an example of how you can use different characters for each hidden character.

:set listchars=eol:^,tab:-,trail:!,extends:>,precedes:<

The character you want to use to denote end of line (eol) is to be typed after eol: in the example command above.

changing the what characters represent the hidden characters

If you look at the video, I copied spaces and pasted it to the very end of line (also known as "trailing spaces") and they are now shown as ! exclamation marks.

Also notice how the default eol character changed from $ dollar symbol to ^ circumflex symbol.

If you want to make this permanent, you can add the same command you ran into your 'vimrc' as follows:

set listchars=eol:^,tab:-,trail:!,extends:>,precedes:<
Basic Vim Commands Every Linux User Must Know [With PDF Cheat Sheet]
A comprehensive guide explaining basic vim commands that will be useful to any Linux user be it a sysadmin or a developer.

Conclusion

This article covers how to enable (and disable) the visibility of hidden characters. We also go over how you can change what symbols denote the presence of a hidden character.

If you are interested in learning more than just the Vim Basics, I highly recommend using this program by Jovica Ilic.

Mastering Vim Quickly - Jovica Ilic
Exiting Mastering Vim Quickly From WTF to OMG in no time
Team LHB