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.

12
  • 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. Jun 2, 2019 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 2, 2019 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). Jun 3, 2019 at 9: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 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, 2019 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, 2019 at 22:14

2 Answers 2

10
+200

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.)

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...

1
  • 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. Jun 5, 2019 at 6:33

You must log in to answer this question.

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