Make GNU Emacs play nicely with a German keyboard layout on Mac OS X

By | January 22, 2014

I used to use Carbon Emacs on OS X for quite a while, but with the release of Emacs 24 I switched to the stock GNU Emacs distribution. While GNU Emacs works fine on OS X, once you throw a German keyboard layout in the mix it doesn’t work so well as OS X uses Option + Number keys for a variety of characters needed for programming like [] and {}. As GNU Emacs uses Option as Meta out of the box that doesn’t work overly well, especially if you do a lot of programming in the C family of languages.

I finally got fed up with this restriction as it seriously affected my enjoyment of cranking out code under OS X. A bit of duckduckgo’ing gave me the necessary settings to use Command as Meta and pass Option through to the underlying OS. From my slightly hazy memory, this was the default layout for Carbon Emacs.

Here’s the code that will switch the keys around:

(setq mac-command-modifier 'meta
      mac-option-modifier 'none
      default-input-method "MacOSX")

Yes, there is also the whole argument about the unsuitability of using a German keyboard layout for programming. A lot of German programmers I know switched to UK or US layout keyboards to make programming easier.

I can drive one of those keyboards if need be but I’m both a touch typist and I rely on Kinesis Advantage keyboards for my day-to-day work thanks to an injury I got from a less than ergonomic desk quite a few years back. And while this old dog is generally happy to learn a new trick or three, I draw the line at switching keyboard layouts when there are other options available.

3 thoughts on “Make GNU Emacs play nicely with a German keyboard layout on Mac OS X

  1. David

    I kept the cmd key as it is, because I like to have some Mac OS X default bindings available. That also that gives me a spare key for own bindings which is nice (e.g. I can use similar keys to switch frames in emacs like in iTerm). And for the few special characters I need to reach via the option key: I just bind them explicitely


    (defun redefine-key (key char)
    (define-key function-key-map key char)
    (global-unset-key key))

    (redefine-key "\M-l" "@")
    (redefine-key "\M-5" "[")
    [...]

    Surprisingly there are no real clashes with important emacs bindings. The only clash is for M-l (=@) which is downcase-word, so I just rebind it to M-L and still can use it, which works fine for me.

    Note that I use global-unset-key here after rebinding the key, because I noticed that minibuffer prompts are quitting otherwise when I enter one of these special characters there (e.g. in an isearch).

    Reply
    1. Timo Geusch Post author

      David,
      thanks for sharing your method of making a German keyboard layout work well with Emacs! Looks like a pretty cool alternative to the method that I posted, especially if you’re more of a hardcore Mac user than I am. My Emacs use predates my use predates my use of Mac OS, so my muscle memory tends to gravitate more towards the “standard” Emacs key combinations, but I can see why having some OS X default key bindings available would make a big difference.

      Reply
      1. David

        I never considered myself a “hardcore” Mac OS user (but maybe I’m already, without knowing…). In fact I used Emacs on a Linux Laptop long before I switched to a Macbook (due to company policy). When I did though, I wanted to learn the “Mac way”, and try to keep the command and option keys as it is. It’s not only about the command, but also about the option key. E.g. in a terminal its convenient to have the same option-d key combination for kill-word then in emacs. (I know readline only supports a small subset of emacs shortcuts, but it’s still a nice overlap.)

        It took some relearning though, because the alt key on a PC keyboard is left of the space, whereas that’s where the command key on the Mac lies. I easily got used to it though, but I wonder what will happen when I switch back to a Linux Laptop.

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *