Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 down vote accepted
+50

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
  do
    syswrite "${line//$'\xC2\xA0'/${bg[red]} ${terminfo[sgr0]}}"
  done
}

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

share|improve this answer
    
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. – mudasobwa 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. – mudasobwa 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

Sure.

$ echo -e "[\xC2\xA0]|[ ]" | cat -A
[M-BM- ]|[ ] 
share|improve this answer
    
I know about cat, thanks. Furthermore I’m able to write my own colorizer, with bj and whores. Unfortunately I’m looking for a terminal-wide setting/solution. So that I switch the option on and yield the visible nb-space in all the outputs/logs/etc. – mudasobwa Sep 9 '13 at 14:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .