Friday, October 22, 2010

Disabling Option-Space (and AltGr-Space) key combination for non-breaking space

Solving “bash: grep: command not found” on OSX and Linux terminals.
How many times this happened to you?

$ find .  | grep something
bash: grep: command not found

This problem affects non US-keyboard users, suffering from sloppy finger.
Typing the pipe ( | ) in a command line requires the combination with Option or AltGr in most non US keyboard, and after the pipe the use of the space is not mandatory but certainly a touch of distinction.
The problem is that by the time when the Space key is hit, some sloppy finger is still firmly pressing the Option key resulting in the Option-Space combination (or AltGr-Space). 
It turns out that the combo produces something that looks exactly like a sp character, but that bash does not recognize as a space. A closer inspection reveals that the Option-Space combination instead of producing the sp byte \040, produces the sequence \302\240 which turns out to be Unicode for non-breakable space.


While working with OSX my solution for several moths was to remove the spaces from the command, recently I found a simpler solution which was described here.
Add the following text to the key binding file (it may not exist yet, if so create it) : ~/Library/KeyBindings/DefaultKeyBinding.dict
  {
    "~ " = ("insertText:", " ");
  }
this file defines the key-sequence mappings for all Cocoa applications. In particular with this line, every time Option-Space is pressed the non-breakable space it is replaced by a regular space. This file can be used for further customizing the behavior of key sequences. The following links provide more information about key binding:
http://hints.macworld.com/article.php?story=2008031611584277
http://www.object.com/TechNotes/DefaultKeyBinding.html
http://www.hcs.harvard.edu/~jrus/site/cocoa-text.html


The solution for Linux is described here. The non-breakable space can be disabled using a xkb option in xorg.conf, adding the following in the InputDevice section related to your keyboard
  ...
  Option "XkbOptions"    "nbsp:none"
  ...
or calling
  setxkbmap -option "nbsp:none"
in a console, or initialization file (~/.bash_profile).


Regarding Unicode, I recommend to read this article: "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)", it gives a brief historical overview and tells pretty much everything one should know about Unicode.

A nice observation from Poul-Henning Kamp, that we don't use of Unicode as part of our programming languages: "Sir, Please Step Away from the ASR-33!".

5 comments:

  1. Awesome, thanks. For linux, it is best however to add the command to one's ~/.xinitrc file, as it only needs to be run on X11 startup.

    ReplyDelete
  2. I suffered for month from this problem! Thank you!

    ReplyDelete
  3. I was about to comment and thank for the tip, until I saw I already did so in 2011.

    Thanks again!

    ReplyDelete