MacBook keyboard hacks for # (hash/pound/numbersign)
by Andrew on Nov.07, 2009, under Tips
One of the few annoying things about my oldish MacBook Pro is its keyboard, for example a few unresponsive keys, but particularly the lack of a # key. It’s a UK keyboard, and has £ for shift-3, and # is hidden in alt-3 (not labelled).
This is fine in native desktop apps, but less fine in some text-mode programs (e.g. vim), when for some reason this often produces a superscript 3 instead.
So I’ve set up a custom keyboard mapping in iTerm to map F3 to #, which works nicely. However!
If I’m SSHed in to a remote Linux machine (or even my local Ubuntu VirtualBox) neither of these keys work in X apps. But, xmodmap (via the config file ~/.Xmodmap) can help. For some reason, Macs all have a dedicated key for these characters — § and ± — which no-one ever uses. But with this line in ~/.Xmodmap we can remap it to produce #:
keycode 18=numbersign
UPDATE: I’ve found a better way which works pretty much globally…
Using Ukelele you can copy the British keyboard layout and then remap keys to your heart’s content. I’ve moved the § character to the alt-§ key combination, in case I ever need it, and moved the # character to the raw § key. This seems to be respected almost everywhere so I don’t need to mess around with alt-3 or F3 any more. Joy. It also works over JollysFastVNC to a remote RealVNC server, which none of the other methods did.
Unfortunately, things still aren’t perfect. If I actually open a VirtualBox console session into GNOME on the local Ubuntu VM, the pointless § and ± key actually produces < and > so neither of these tricks work. In fact, I can’t get anything to generate a # even though I have the MacBook Pro Intl keyboard layout selected in GNOME. Any ideas?
UPDATE 2: YES!! I’ve finally cracked it for VirtualBox. With the help of the xkeycaps command, I discovered that X the keycodes coming into Ubuntu weren’t what I thought they were — somewhere the Mac-ness of the keyboard layout was getting lost. It turned out that the § key was generating keycode 94 instead. So I set up this in .Xmodmap on the Ubuntu VM:
keycode 94=numbersign
Now it works in VirtualBox too. Leave gifts of thanks below