OS X: Aquamacs and GNU Emacs — Keyboard Problems on the Mac

If you are — like I — one of those strange people who are not using a keyboard with US layout on a Mac, you might encounter a real problem while using the apps Aquamacs and GNU Emacs on OS X. Depending on your keyboard layout, you might not be able to enter special characters like  [, ], |, {, }, and @. The reason is that Apple’s alt/option key is used as meta key in Aquamacs and GNU Emacs. Here are two solutions which will solve this problem.

On OS X I used Emacs in the terminal windows primarily. Emacs worked fine there. ⌥ worked OS X like and is not used as meta key. So on my keyboard I can, for example, enter @ by pressing ⌥ + L. As meta key I can use ESC which is not so comfortable as an Alt key on a PC but I can live with that. On other platforms I have to use the escape key as meta key as well.

But when it comes to the apps Aquamacs and GNU Emacs (a.k.a. Emacs.app) the trouble started. These apps are really nice. You can use your mouse, click on menus, and — hey! These apps are colorful. 😉 But after this aesthetic and positive first impression a real problem occurred: ⌥ works no longer as expected on OS X. It’s used as meta key. When I press ⌥ + L Emacs tells me discretely and politely that “⌥l is undefined”. Thank you Emacs! — Time to bring these Emacs apps into a working and useful state.

Different Emacs Versions on OS X.

Figure 1: Different Emacs Versions on OS X.

Aquamacs’ Solution

Aquamacs has a special menu command to enable the special ⌥ sequences for some keyboard layouts. In the submenu Options → Option, Command, Meta keys (see figure 2) you will find several options to customize Aquamacs to support your keyboard layout. If your keyboard layout is supported you are done. If your keyboard’s layout is not supported, you have to use the same solution as with GNU Emacs which needs a bit more work.

Figure 1: Aquamacs Meta Key Settings.

Figure 2: Aquamacs Meta Key Settings.

A Solution for GNU Emacs

Unfortunately GNU Emacs does not provide such a useful option. So we need here another solution. The following solution will also work if your are using Aquamacs with a keyboard which is not supported by Aquamacs.

The very simple solution is using global key settings in your ~/.emacs file. You just have to map meta key sequences to insert the right character. For example: If your @ sign is located at ⌥ + L on your keyboard you will have to insert the following lisp command into your ~/.emacs file:

(global-set-key "\M-l" (lambda () (interactive) (insert-string "@")))

This maps the sequence meta key (\M) + l to  an anonymous function which insert the string "@" at the current position of the cursor.  Whenever you press M-l, which is equal to ⌥ + L in our case, it will insert the @ sign into your current buffer. (You will have to Emacs reload your ~/.emacs, of course; e.g. by restarting Emacs.)

Parallel Use of App and Terminal

By using the last mentioned generic solution your Emacs app will work fine. But what is with your Emacs in the terminal window? — It have got the same settings even if it does not need it, because it uses the same file ~/.emacs for configuration. Time to find a solution for this, too.

If you call the function emacs-version your Emacs will respond with its version and implementation. You can try it by pressing M-x and typing in

emacs-version

and press the enter key.

GNU Emacs.app will respond with something similar like

GNU Emacs 24.3.1 (x86_64-apple-darwin, NS apple-appkit-1038.36)
of 2013-03-13 on bob.porkrind.org

Aquamacs will print a string such as

Aquamacs 3.0a GNU Emacs 24.3.50.2
(x86_64-apple-darwin13.0.0, NS apple-appkit-1265.00)
 of 2014-01-21 (Aquamacs-3.0a) on watson

Last but not least GNU Emacs in the terminal windows will print information like these:

GNU Emacs 22.1.1 (mac-apple-darwin)
of 2014-04-18 on osx102.apple.com

With these information it is easy to distinguish the different Emacs versions on your OS X. Instead of mapping keys in ~/.emacs in all cases you can use the following construct to apply your settings only in the case of Aquamacs or Emacs.app:

(if (string-match "Aquamacs\\|NS apple-appkit\\|NS appkit" (emacs-version))
  (progn
    ; your global-set-key calls go in here...
  )
)

Bring it All Together

Now we just need to bring it all together so that our Aquamacs and Emacs.app respectively as well as our Emacs in the terminal will work properly. Here are some examples for a few keyboard layouts.

By the way: If you want to define new mappings Mac’s Keyboard Viewer is very useful to find yet undiscovered keys. Apple did not label all the useful keys on your keyboard. 😉

Here is an example for a German keyboard:

(if (string-match "Aquamacs\\|NS apple-appkit\\|NS appkit" (emacs-version))
 (progn
 (global-set-key "\M-l" (lambda () (interactive) (insert-string "@")))
 (global-set-key "\M-n" (lambda () (interactive) (insert-string "~")))
 (global-set-key "\M-5" (lambda () (interactive) (insert-string "[")))
 (global-set-key "\M-6" (lambda () (interactive) (insert-string "]")))
 (global-set-key "\M-7" (lambda () (interactive) (insert-string "|")))
 (global-set-key "\M-8" (lambda () (interactive) (insert-string "{")))
 (global-set-key "\M-9" (lambda () (interactive) (insert-string "}")))
 (global-set-key "\M-/" (lambda () (interactive) (insert-string "\\")))
 (global-set-key "\M-e" (lambda () (interactive) (insert-string "€")))
 (global-set-key "\M-$" (lambda () (interactive) (insert-string "£")))
 )
)

This is a sample for a Swiss-German keyboard:

(if (string-match "Aquamacs\\|NS apple-appkit\\|NS appkit" (emacs-version))
 (progn
 (global-set-key "\M-g" (lambda () (interactive) (insert-string "@")))
 (global-set-key "\M-n" (lambda () (interactive) (insert-string "~")))
 (global-set-key "\M-5" (lambda () (interactive) (insert-string "[")))
 (global-set-key "\M-6" (lambda () (interactive) (insert-string "]")))
 (global-set-key "\M-7" (lambda () (interactive) (insert-string "|")))
 (global-set-key "\M-8" (lambda () (interactive) (insert-string "{")))
 (global-set-key "\M-9" (lambda () (interactive) (insert-string "}")))
 (global-set-key "\M-/" (lambda () (interactive) (insert-string "\\")))
 (global-set-key "\M-e" (lambda () (interactive) (insert-string "€")))
 )
)

Finally here is an example for a French keyboard:

(if (string-match "Aquamacs\\|NS apple-appkit\\|NS appkit" (emacs-version))
 (progn
 (global-set-key "\M-n" (lambda () (interactive) (insert-string "~")))
 (global-set-key "\M-\(" (lambda () (interactive) (insert-string "{")))
 (global-set-key "\M-\)" (lambda () (interactive) (insert-string "}")))
 (global-set-key "\M-5" (lambda () (interactive) (insert-string "[")))
 (global-set-key "\M-°" (lambda () (interactive) (insert-string "]")))
 (global-set-key "\M-L" (lambda () (interactive) (insert-string "|")))
 (global-set-key "\M-/" (lambda () (interactive) (insert-string "\\")))
 (global-set-key "\M-$" (lambda () (interactive) (insert-string "€")))
 )
)

You can use these settings directly by simply adding them at the end of your ~/.emacs. It offers you also the way to customize Emacs.app and Aquamacs for your own keyboard layout or by defining additional keys.

This entry was posted in Emacs, Mac, OS X and tagged , , , , , . Bookmark the permalink.

4 Responses to OS X: Aquamacs and GNU Emacs — Keyboard Problems on the Mac

  1. Fabien says:

    Hi there,
    thank you for the tips,
    the particular command
    (global-set-key “\M-°” (lambda () (interactive) (insert-string “]”)))

    does not work on my mac (error: Invalid modifier in string)
    it’s seems to be a problem with only the “°” key
    do you know what is happening ?

    thanks

    • Hi Fabien,

      Thank you for your comment.

      Strange in my configuration it worked.

      Could you do the following, please?

      -> Press C-h k to describe key.
      -> Then press M-°.
      -> What does your Emacs tell you about this key?

      BTW: What Emacs do you use — Aquamacs or GNU Emacs?

      Kind regards,
      Oliver

  2. Muad says:

    I am using Aquamacs with a german keyboard and I stumbled upon an additional problem.

    I am using org-mode, and one of the commands is C-c C-x \

    After hitting C-c and C-x Aquatics recognises \ as option+7. It is actually written option + shift + 7

    Any idea how I could fix this?

    • Muad,

      Yes, I see the problem with Aquamacs as well as with GNU Emacs on OS X.

      Your key sequence is recognized as C-c C-x M-/ on OS X instead of C-c C-x \ on PC-based Unix/Linux and Windows.

      Simply redefine your key binding for C-c C-x M-/ in org-mode.

      My guess is that this key sequence should call (org-timer-pause-or-continue). Then this LISP code in your ~/.emacs should work:

      ;; Define a function which should be called a load of org-mode.
      (defun my-org-mode-config ()
         "Set key bindings for org-mode on OS X."
         (local-set-key (kbd "C-c C-x M-/") 'org-timer-pause-or-continue)
      )
      ;; Add a hook for org-mode to call this function.
      (add-hook 'org-mode-hook 'my-org-mode-config)

      Let me know whether it works, please.

      Kind regards,
      Oliver

Leave a Reply to Fabien Cancel reply

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