I know that I can use :set list to show invisible characters. However, I am interested in a thing I've seen in many other editors, which is not to show or not show invisibles outright but show them only while typing

For example, when I press Tab it shows me the char I've assigned, but as soon as I start typing again, the character disappears. Same with space, etc.

Is there any known way to do this in Vim?

  • 1
    I'm not sure if what you saw in other editors isn't something actually unrelated with set list, but instead something which has to do with showing trailing tabs or spaces, or showing mix of tabs and spaces. This, Vim can do, by using for example something like: github.com/tpope/vim-sleuth/issues/13#issuecomment-50433307 (there are many solutions). Such rules may have the secondary effect that, in insert mode, if you type a tab or a space, you will see it highlighted/displayed differently, until you type some other character. – VanLaser Feb 6 '16 at 11:59

You can achieve an effect similar to this by changing the colour of tabs so you can't see them, and then making them visible again when they are at the end of a line.

In the colorscheme I'm currently using, the background is set to colour 8. You will need to tweak the colours in the first highlight command to match those in your colorscheme (and add gui options to the highlight commands if you're working in GUI Vim).

" First, turn on list
set list

" Now change the colours so that tabs blend in with the background
highlight SpecialKey ctermfg=8 ctermbg=8

" Now add a highlight group for tabs we do want to be visible.
" The command below is likely to work in all colorschemes, but feel free to
" edit to your taste.
highlight link VisibleTab Error

" Set up a match to use the new highlight for tabs at the end of the line
match VisibleTab /\t$/

You can also use the same technique for some of the other invisible characters mentioned in :help 'listchars' by changing the regular expression in the :match command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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