Show nonprinting characters in Vim
Posted on Sunday, June 14, 2015 by TheBlackzone
When editing tabular data or regular text, it is often quite handy to have a visual representation of nonprinting characters like EOL or TAB. To achieve this, Vim provides us with the listchars
option, a feature that has gone unnoticed by me for a long time...
Say Hello to the listchars option
The listchars
option allows us to set replacements characters for
- End of line (EOL) markers, which are typically newline characters like
CRLF
- Tab characters
- Spaces
- non-breakable spaces
- Trailing spaces at the end of a line
- Concealed text, a feature of Vim syntax coloring to hide parts of a text
- So called Precedes and Extends which are shown to the left and right of a line when the
wrap
option is turned of and the line is to long to be fully displayed
Vim also supports different colors for the replacement characters via the NonText
and SpecialKey
syntax coloring options, both of which are honored by most of the popular color schemes.
_figure(vim-listchars.png, `listchars replacements')
For a full explanation of this features see Vim's help by typing :he listchars
.
Setting the replacements
I use replacements for EOL and Tab and have set the listchars
option via the following line in my .vimrc
:
set listchars=tab:▸\ ,eol:¬
The symbols here are Unicode characters and can be entered by typing the sequence <CTRL-V>u25b8
for the '▸' symbol (Unicode character U25B8) and <CTRL-V>u00ac
for the '¬' symbol (Unicode character U00AC). Also note the blank (space
) after the backslash.
In order to make this work you will also have to enable Unicode support by setting the following options in your .vimrc
:
scriptencoding=utf-8 set encoding=utf-8
Of course you can use other characters here. Personally I like the shown symbols for beeing quite unobtrusive.
Turn it on
I wanted the replacements to be shown only if needed so I initially turned them off by setting
set nolist
in my .vimrc
.
It can be turned on by the command
:set list
and we can easily toggle between it by the command
:set list!
which I mapped to the F7 key in my .vimrc
:
" Toggle listchars noremap <F7> :set list!<CR> inoremap <F7> <ESC>:set list!<CR>i
The bottome line
So, to sum it up, here are the relevant lines I have set in my .vimrc
:
set listchars=tab:▸\ ,eol:¬ set nolist noremap <F7> :set list!<CR> inoremap <F7> <ESC>:set list!<CR>i
All in all this is another 'not-so-widely-known' feature of Vim which is quite useful and I hope this short roundup of it is helpful to you.
Tags: vim