Can I configure bash/readline to automatically convert NBSP to normal space?

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.


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