69

How can I configure vim to display hidden characters like tabs or spaces ONLY while those characters are typed? I do not want tabs and/or space replaced with special characters like ">" or "_" when the file is written.

  • 2
    Do you definitely need spaces to show? The typical solution is to use :set list. That can get everything except spaces. – Kris Harper Nov 1 '11 at 18:23
77

You can use the commands

:set listchars=eol:$,tab:>-,trail:~,extends:>,precedes:<
:set list

to show all characters that aren't whitespace. So spaces are the only thing that doesn't show up.

If you absolutely need spaces to be marked as well, you'll need to try something less nice. Something like

:%s/ /█/g

Will replace all spaces with a block character. Then you'd need to undo it before writing. You could remap your write command do

cmap :w :%s/█/ /g<CR>:w

Just a suggestion. I haven't tried that though.

| improve this answer | |
  • Thanks for the help; your suggestion works quite nicely. However, I've noticed that at times when I open certain files I see ">---" (indicating tab). Is there a way to make it so that the tab characters show only during typing? – dan0 Nov 1 '11 at 21:02
  • 1
    @dan0 You mean only during insert mode? I don't know. I kind of doubt it. You could make some mapping functions so that when you go into insert mode, it first calls the :set listchars command and sets it to nothing, then calls it again when you leave insert mode. But I don't know enough about maps to do that. – Kris Harper Nov 1 '11 at 21:13
  • @JonathanKomar "set nolist" – Vladislavs Dovgalecs Aug 16 '17 at 0:46
  • 1
    Using space:␣ works well for me: :set listchars=eol:$,tab:>-,trail:~,space:␣ – loxaxs Feb 3 '18 at 20:57
25

More recent versions of vim can show spaces as well. (Confirmed on vim 7.4.1689).

:set list
:set listchars=tab:→\ ,space:·,nbsp:␣,trail:•,eol:¶,precedes:«,extends:»
| improve this answer | |
  • 1
    Best answer, most comprehensive list of invisible characters here. – Lambart Feb 13 at 19:35
6

In my case I had to fight nbsp,

:set listchars=nbsp:☠,tab:▸␣
:set list

You can also use ␣ instead of ☠

Here is an example where the above vim config highlights the "invisible weird" spaces and additionally it highlights tabs (red color because I am using trailing-whitespace vim plugin)

Here

| improve this answer | |

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.