Skip to main content

The Strange Case of Emacs, # and the UK Keyboard

Programmers typing on the UK Mac keyboard have a problem with #

# is commonly used in programming languages and the Mac UK layout puts # on ⌥ 3 (Alt-3), as opposed to Shift-3 on the US layout or Windows UK layout.

This is a particular problem in Emacs, which interprets the ⌥ (Alt) key press as the Meta key and so Emacs receives M-3 instead of #.

My first approach was just to make M-3 insert #:

(define-key brf-mode-map "\M-3" (lambda () (interactive) (insert ?#)))

This hoses bookmark 3 in brf-mode, but losing one bookmark seemed like a compromise I could live with.

However it turns out this was a pretty dumb idea (😖), as it stops all the uses of # other than inserting, for example searching for # ! It also stops me typing the command for server-edit (C-x #), which is a very common usage for me.

Looking around the web, people had some "creative solutions" for this problem:

Inserting # handling into all the various keymaps

This seems to be the most popular "solution", but for me this is like playing "whack-a-mole" and doomed to failure (apart from being really ugly). You really want to do the translation at a much earlier stage in the input chain.

Pretending to be Australian

The Mac Australian layout is identical to the UK layout, but # is back on Shift-3 (and £ is removed). While this works well unless you want to type a £, having the Australian flag permanently displayed on my Mac menu bar is step too far for me 😀

Changing Low-level Keyboard Translation

This looks a lot more promising, as the earlier in the input chain you do the translation the better:

(define-key key-translation-map "\M-3" "#")

This still hoses Bookmark 3 for me, but will work well for most people.

Make Emacs ignore Right Alt

Emacs for MacOS has some specific settings of how the meta keys are handled. One of which is to ignore the key press (ie don't interpret it as a meta key press).

So I decided to ignore the right ⌥ (Alt)  key with:

(setq ns-right-alternate-modifier (quote none))

This means I can use right ⌥ (Alt) for # and € and retain left ⌥ (Alt) for normal Emacs usage, like brf-mode Bookmark 3.

This is a great solution for my usage, but your mileage my vary!

Comments

Popular posts from this blog

Merging Git Repositories

No project of significant size that I've ever seen has retained its initial structure. Restructuring projects is a fact of life, but unfortunately Git doesn't make it easy. Fundamentally this stems from the way Git works, treating changes as a succession of snapshots and not storing any other metadata. Of course this is part of what makes Git fast and efficient, but at the expense of making some common operations more difficult for users. Git really is a perfect 21st century illustration of the classic  "Worse Is Better"  paradigm of successful software 😀 Previously I discussed how to split a Git project apart into separate repositories . Now I'm going to discuss how to do the opposite and merge separate repositories into one. On the face of it, this would seem a simpler task as Git has powerful support for merging... Let's take the opposite example to my splitting apart article - say you have a main Git repo (ProjA) and a second repo (ProjB) in...

Black Hole Marsh

We also visited Black Hole Marsh nature reserve, which is a large saltwater lagoon having two large bird hides, one in the middle of the lagoon  (Island Hide) and the other overlooking both the lagoon and the Axe Estuary (Tower Hide). There were many birds to be seen, but just as at the Seaton hide, a 400mm lens was not really enough to get frame-filling shots of the birds. All the following taken with Nikon D700, 70-200 f/2.8G VR II and TC-2E III converter (400mm focal length equivalent): Heron Fishing on the Lagoon with the Island Hide in the background Axe Estuary from the Tower Hide Pair of Bar-tailed Godwits Little Egret Mallards on the Lagoon Lapwing Fallow Deer  and Egret on the banks of the Axe Pied Wagtail on the thick green algae of Black Hole Marsh. Rat enjoying leftovers from the bird feeder

My Work in new Top Trumps Birds of Prey Pack

The new Top Trumps "Birds of Prey" pack has my picture illustrating the Secretary Bird card 😀 Here's the original picture: From Flickr