I’m looking for possibility to somehow highlight the non-breakable space (\xA0) in the shell output to distinguish it from the normal space during debug sessions. E. g., I would like to see the output of $ echo -e "[\xC2\xA0]|[ ]" like:

[_]|[ ]

Colors, underscores, even blinking is OK. I even can switch from zsh to any shell where it’s configurable enough. Is it at least possible?

| |

This highlighting is IMHO more easily implemented in the terminal emulator than in the shell. And as a benefit you get the highlighting independent from the shell -- so it's even working with less etc. Since you are fine with switching the shell, I suppose you are fine with switching to URxVt, too.

In conjunction with the perl extentions urxvt-perls it is possible to make URLs etc. clickable. But we can abuse this feature just to underline the non-breakable spaces.

The configuration is done in ~/.Xdefaults:

URxvt.perl-ext:           default,matcher
URxvt.matcher.button:     2
URxvt.matcher.pattern.0:  (\\240)
URxvt.matcher.launcher.0: /bin/true
  • line 1 activates the appropriate extension
  • line 2 configures the usage of the middle mouse button for clicking on the ,,hyperlink''
  • line 3 defines the regex to match, here every non-breakable space; A0 (hex) = 240 (oct)
  • line 4 defines the action, in your case you don't want to open up a browser or any program, so /bin/true should be fine

Reload ~/.Xdefaults by restarting the X server (this might be overkill, but I'm not sure if a xrdb ~/.Xdefaults is sufficient...) and you get your desired result. With a magnifying glass it is even possible to distinguish between non-breakable spaces an underscores (notice the shift by 1 pixel) ;):

enter image description here

The following is kind of a proof of concept for a "backend" solution. Be aware, that this will break a lot of things, namely interactive programs like man, less etc. (vim is complaining but still working). This approach was posted by Atom Smasher on the zsh-users mailinglist on Sat, 16 May 2009 to colorize STDERR. I adopted it to your requirements:

# ## highlight_nbs.zsh ##

zmodload zsh/terminfo zsh/system
autoload colors is-at-least

if [[ "${terminfo[colors]}" -ge 8 ]] { colors }

highlight_nbs() {
  while sysread line
    syswrite "${line//$'\xC2\xA0'/${bg[red]} ${terminfo[sgr0]}}"

precmd() { sleep 0 }

## i'm not sure exactly how far back it's safe to go with this
## 4.3.4 works; 4.2.1 hangs.
is-at-least 4.3.4 && exec  > >(highlight_nbs)

The tricky part is exec > >(highlight_nbs) which processes the complete output of the replacing shell through the highlight_nbs function. The precmd definition is necessary to avoid a race condition, otherwise the new prompt is printed before the actual output of the executed command.

Here is a demo:

enter image description here

| |
  • Thank you. It seems to be a quirk-mode solution. While it likely might solve the problem temporary on per-debug-session basis, it still hardly helps with remote (say, ssh) sessions. That’s kinda frontend solution and I still hope to find a backend one. But thank you anyway, this at least solves me the problem for the moment. – Aleksei Matiushkin Sep 9 '13 at 20:08
  • @mudasobwa: Perhaps you can elaborate on your requirements in your question. From my point of view a frontend solution (how you call it) is even superior as it's independent of the system you are logged into (via ssh or what-so-ever). As long as you work with "your" computer and urxvt you get the highlighting. Or is it the other way round: You log into one remote system from different machines?! – mpy Sep 14 '13 at 15:18
  • yes, in general the problem to be solved is following: I have an amount of computers as ssh-clients, and a bundle of ssh-hosts. I would like to have kinda “yadr-like” setup on remotes. – Aleksei Matiushkin Sep 16 '13 at 6:27
  • @mudasobwa : I added an idea for a "backend" solution. It is far from being perfect, but perhaps it's of some use for you. – mpy Sep 17 '13 at 9:40


$ echo -e "[\xC2\xA0]|[ ]" | cat -A
[M-BM- ]|[ ] 
| |

If you are on a Mac you might consider using iTerm with this hack. It is also shell-agnostic.

It changes the background color of an NBSP, using the 'Trigger' feature in iTerm.

| |
  • Please quote the essential parts of the answer from the reference link(s), as the answer can become invalid if the linked page(s) change. – DavidPostill Feb 16 at 10:59
  • I clearly stated in tags I am using the robust operating system, not MacOS. – Aleksei Matiushkin Feb 17 at 7:02

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.