11

I'm using Finnish keyboard layout which maps AltGr+Space to non-breaking space (NBSP, U+00A0). I'm happy with this in general but I find that I often accidentally write NBSP instead of normal space after pipe "|" symbol (written with AltGr+< with Finnish keyboard layout) while using bash command line. I guess this is caused by the fact that I need to hold AltGr while typing the pipe and release it before hitting space. And when I have bad timing for the release of AltGr I end up with invisible typo on the command line and error messages such as

 grep: command not found

which looks pretty similar to

grep: command not found

which makes this issue a bit hard to notice on the first time.

I know that I can disable NBSP but I would prefer disabling it (having AltGr+Space to produce regular space) only after pipe character, or if that's not possible, always on the bash command line or readline level. Is there any simple way to do this without modifying source code of bash, readline or my terminal emulator (gnome-terminal)?

Another good solution would be to configure NBSP to be somehow visible on the command line, e.g. replaced with another character (say U+2423 ) for rendering only.

dessert
  • 39,392
  • 12
  • 115
  • 163
Mikko Rantalainen
  • 3,200
  • 1
  • 24
  • 36
  • 1
    @dessert no need for a bounty, really. I just hit the problem and wrote a question about it. Later I found close enough config via Google Search to figure out the answer below. I'm still looking for an answer to make NBSP visible instead of disabling the character for all cases. I think I would like to have visible NBSP on readline level so that all terminal output is not messed up if I have NBSP in data strings. – Mikko Rantalainen Jun 02 '19 at 10:41
  • @MikkoRantalainen could you explain what you're still missing more clearly? Since you found the way to map characters, why isn't `" ":␣` enough? What else do you need? – terdon Jun 02 '19 at 13:43
  • @dessert I would love to have a feature where I could write NBSP on the command line and get `echo a␣b` but the resulting output line were `echo a b` with real NBSP in the output. As such, I think it must be done on the readline level (that is, command line *editor* always displays visible character but terminal emulator cannot know the difference between editor display and output display and therefore the terminal emulator cannot do the visual replacement for the rendering). – Mikko Rantalainen Jun 03 '19 at 09:12
  • 1
    Solving it on readline level is indeed nice, but you might hit the same problem while writing a shell script in vim/emacs/etc in the terminal. See https://bugzilla.gnome.org/show_bug.cgi?id=788673 for a working proof of concept patch for gnome-terminal to highlight string matches, including perhaps the single NBSP character. (Yeah I know you said you didn't want to modify any source code…) – egmont Jun 16 '19 at 22:11
  • 1
    Another approach is to address it on the font level, via a special font where NBSP's glyph is not empty, along with some fontconfig configuration. This would also work for graphical editors, not just the terminal. Yet another approach is to modify the keyboard layout to emit regular space, and use some other method to enter NBSP in the rare case when that's what you need. – egmont Jun 16 '19 at 22:14
  • @egmont I guess getting the source code patch upstream does count as not needing to modify the source code in the long run. Perhaps I should have written "without doing local modifications to the source code". – Mikko Rantalainen Jun 19 '19 at 14:15
  • I cannot tell if this feature will get accepted to gnome-terminal (or vte only and then another vte-based emulator) or not. Even if it gets accepted, there's still a lot of work to do before it becomes an upstream feature. That's why I pointed you (and other readers) to this proof of concept patch. For some people (not necessarily including you) this feature might worth the trouble of patching and compiling. – egmont Jun 19 '19 at 16:33
  • @egmont some apps can show regular space and non-breaking space differently already. Those include LibreOffice Writer and Geany (via "show white space" editor preference, though the visual difference may be hard to see on the latter. [bug report](https://bugs.launchpad.net/ubuntu/+source/xkeyboard-config/+bug/218637) – jarno Aug 22 '19 at 08:17
  • Note that some Finnish keyboard layout variants such as Classic map AltGr+Space to Space. – jarno Aug 22 '19 at 08:21
  • @jarno There are other cases that would be truly cumbersome to highlight in the terminal, such as various zero width control characters, which can also easily confuse users. From a practical view, replacing "special spaces" with "␣" in the terminal could help the user in many (but not all) cases. From a theoretical view, I'm not convinced it should be the terminal doing this, rather than the shell, editor etc. which can decide to display arbitrary more verbose things, including for zero width characters. – egmont Aug 22 '19 at 12:23
  • @jarno Geany's "show white space" marks "real" spaces with a barely visible middle dot, and not the "special" ones. Even if the dot was clearly visible, this is the exact opposite of the reasonable behavior that helps you catch accidental "special" spaces. – egmont Aug 22 '19 at 12:37
  • @egmont I found that in Padauk fonts do not have various no-break spaces defined, so they appear as empty rectangles. But the fonts are not monospace, so they are not that suitable as terminal font. – jarno Aug 23 '19 at 13:31

2 Answers2

10

This can be done on the readline level two different ways.

Method 1

Put following in .inputrc (the configuration file for readline):

# include default system config because ~/.inputrc overrides system config
$include /etc/inputrc
# map NBSP to regular space (left part has NBSP in quotes, right part has space)
" ":" "

If markdown messes up the above, you have to put NBSP in quotes on the left side of colon as explained in the comment. This will map any occurrence of NBSP on input stream with a regular space.

Method 2

Put following in .inputrc:

# include default system config because ~/.inputrc overrides system config
$include /etc/inputrc
# map "pipe + NBSP" to "pipe + regular space" (left part has NBSP in quotes)
"| ":"| "
set keyseq-timeout 250

The idea is to map key sequence {pipe followed by NBSP} to {pipe followed by space}. This works if you type the sequence within 250 ms (configurable above). However, until the timeout is gone, typing pipe symbol alone will not output anything. And if you type the sequence too slow, the fix will not be applied. Also note that the timeout is global so if you intend to use any other sequences, you have to set timeout long enough to be able to type the longest sequence. (The readline library is not clever enough to allow typing the characters and later replace already visible characters after the character sequence matches the configuration.)

Mikko Rantalainen
  • 3,200
  • 1
  • 24
  • 36
0

There's an excellent article dealing with this problem:

I've been attempting to address the primary concern with grep error messages and changing NBSP to something more visible using exec command. I haven't got the syntax down pat yet though:

exec 2> >(tr $'\xa0' $'\x43' >&2) 

The idea is to have this command in ~/.bashrc so it automatically loads with gnome-terminal opening. But as I said it's not working yet...

WinEunuuchs2Unix
  • 99,709
  • 34
  • 237
  • 401
  • 1
    That would only deal with the error messages. I would prefer to catch the problem while I'm editing the command line because I might be writing something like `perl -i -npe 's/MARKER/4 KB/'` and I accidentally slip a NBSP between the `4` and `K`. Oops, the problem gets stored into a file but I don't get any error and the command line I wrote looks perfect. – Mikko Rantalainen Jun 05 '19 at 06:33