Currently I highlight these with:

set listchars=tab:▶—,trail:␣,nbsp:⍽

But the drawback is I cannot usefully copy-paste from the terminal where vim resides to another one: e.g. suppose I'm editing a shell script and want to test some of the lines in a shell, it will choke on the 's and 's.

So I'm looking into highlighting these "listchars" by colors. I found the following trick:

set listchars=tab:\ \ ,trail:\ ,nbsp: 
:highlight SpecialKey ctermbg=7

This is quite good, however they all have the same color. Is there a way to display them in different colors? This may be relevant but I'm not vim-savvy enough to adapt it to my case :)

  • 1
    I will tentatively say "not possible" due to this line in $VIMRUNTIME/doc/todo.txt: "7 SpecialKey highlighting overrules syntax highlighting. Can't give an unprintable char another color. Would be useful for ^M at end of line." Is there no workaround at all? Needs some investigation.
    – B Layer
    Feb 2 '19 at 4:59
  • 1
    Note end of 'listchars' help it says The "NonText" highlighting will be used for "eol", "extends" and "precedes". "SpecialKey" for "nbsp", "space", "tab" and "trail". So you can at least differentiate those two groups out of the box. But the "todo" entry doesn't give me much confidence in anything beyond that.
    – B Layer
    Feb 2 '19 at 5:08
  • @BLayer thank you for your investigation. Could the answer linked in my question be extended to avoid listchars altogether and rely on syntax highlighting only?
    – L. Levrel
    Feb 2 '19 at 10:21
  • 1
    Two possible solutions for fixing your copy-paste issues: temporarily :set nolist to turn off displaying the listchars, or use vim's clipboard integration to copy using vim itself, rather than selecting on the terminal level.
    – user4879
    Feb 2 '19 at 12:22
  • 1
    Yeah, @swalladge has a point. If copy-paste is the root problem try to address it directly. Put the effort into curing the disease as opposed to treating the symptoms. ;) (Unless you wanted different colors for other reasons, too.)
    – B Layer
    Feb 2 '19 at 14:04

This appears to be an example of the XY problem. @b-layer has pointed out in the comments that there is likely to be limited success with individually highlighting listchars. I will address the issues with copy-pasting from vim in a terminal.

Option 1: continue using terminal-level copying. To avoid listchars getting into the copy, temporarily to :set nolist to stop vim from displaying listchars. Then you can copy without any issue.

Option 2: use vim's system clipboard integration to copy directly from vim. See :help clipboard for more information. Basically, if vim is compiled with clipboard support, the * and + registers can be used as the primary and clipboard x11 clipboards respectively. For example, "+yy will copy the current line to the clipboard, and "*p will paste the primary selection into vim.

To use the system clipboard by default instead of needing to use the * or + registers explicitly, use :set clipboard=unnamed or :set clipboard=unnamedplus. See :help 'clipboard' for more.

EDIT: just thought of something (which i realize now is exactly what the answer you linked to was talking about). You can actually highlight these characters using a different method. Instead of relying on list, you can add custom syntax matching and highlight groups. For example the following vimscript:

set nolist
hi  TabChar             ctermbg=1
hi  TrailingSpaceChar   ctermbg=2    
hi  NBSP                ctermbg=3
syn match TabChar " "
syn match TrailingSpaceChar " *$"
syn match NBSP " "

Will result in (solarized colour scheme):

enter image description here

Note: if copy/pasting this script, you will probably have to manually write in the tab and nbsp characters in the match regex because browsers like to convert them to spaces...

  • 1
    Excellent! I wish I could upvote twice :)
    – L. Levrel
    Feb 3 '19 at 10:44
  • So what the heck are they talking about in the todo.txt entry? This is exactly what I was thinking but was dissuaded by "SpecialKey highlighting overrules syntax highlighting". Perhaps they meant "Unless 'list' is disabled SpecialKey highlighting...etc". Guess next time I'll try it for myself. :)
    – B Layer
    Feb 4 '19 at 13:26
  • Yeah I suspect it means that listchars and list and the associated SpecialKey highlight group overrides any other highlighting rules. It doesn't stop you from highlighting them if 'list' is off though.
    – user4879
    Feb 5 '19 at 5:43
  • Also to note: it is possible in neovim to have 'list' on and custom highlighting.
    – user4879
    Feb 5 '19 at 5:44

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.