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

Renaming Files in Git

Renaming Files I recently had to rename a lot of files in brf-mode for submission into MELPA . These files have 10+ years of version control history I wanted to keep. In the process I realised retaining all that history in Git isn't as simple as I thought 😀 I thought it was as simple as using git mv rather than filesystem  mv and Git would know everything had been renamed. It turns out I was wrong and I should have known that from my knowledge of how Git works 😖 In reality, Git works by storing snapshots rather than file or directory metadata and git mv is just a convenience shortcut for typing: $ mv <old name> <new name> $ git rm <old name> $ git add <new name> That's all there is to it! Now the "magic" happens when you git log or git blame a file and Git works out the file was renamed by diffing the contents.  If files have the same contents (within a certain threshold %) it thinks a rename happened. See here in the  Gi

Setting Environment Variables and the PATH on MacOS

Time Was setting environment variables and the PATH on a Mac running OSX was just like any other *IX.  However with successive OS releases Apple have changed how this works (more than once) and generally made it more difficult 😢  This article discusses how I go about setting environment variables on Mojave and Catalina. Why does this matter? MacOS doesn't add  /usr/local/bin to the PATH by default, which is unfortunate as most *IX-style programs you build yourself will be installed in there. If you only ever launch stuff from Terminal, all you have to do is set environment variables and the path from Shell startup files in the time immemorial fashion. However, this doesn't help with native Mac Apps like Emacs, which aren't launched from a shell and where you may still want to access custom environment variables and programs in /usr/local/bin . Setting the PATH In the past you could add to the path via /etc/paths (or paths.d ), however this no longer works i

More Axe Birdwatching

We went back to the Seaton bird hide the next day, there were plenty of birds but the weather was poor. Cold and very dark and not at all suited to photography. Here's the best I managed (a ll taken with Nikon D700, 70-200 f/2.8G VR II and TC-2E III converter (400mm focal length equivalent): Male Pheasant Enjoying Leftovers from Bird Feeder Little Grebe  (I got quite close to this, but it's a very small bird!) Cormorants Flying along the Axe