Redefine the X11 Meta Key in Mac OS X

Today I finally got fed up with the traditional X11 keyboard mapping in Mac OS X. I’m talking specifically about the meta key. Any Mac user who has not delved into the wonderful world of X11 will not know what a meta key is, so let me give some background here. The meta key is a function key, similar to the command key (on Macs) or the ctrl key (on Windows machines).X11 Its main use is in my favorite text-editor, Emacs, which requires X11 to run. The Emacs philosophy is something that would take hours to discuss, but in short, Emacs users believe that you shouldn’t have to move your hands from the home row typing position when editing text. No use of the mouse or the arrow keys is required. All moving around, deleting text, cutting, copying, etc. is done with a combination of the control key, the meta key, and the standard letters. Check out some of these common Emacs keyboard shortcuts to get an idea of what I’m talking about. Productivity is curbed in Emacs when there is no meta key or when it is in an unfamiliar keyboard spot.

There is a way to switch which key that Mac OS X recognizes as meta by using an X11 keyboard mapping preference file. First, uncheck the “Use the system keyboard layout” option in X11 preferences. You may also have to disable the “Enable keyboard shortcuts under X11″ option as well. Then, create (or add to) the file ~/.Xmodmap and insert the following six lines:

clear Mod1
clear Mod2
keycode 63 = Mode_switch
keycode 66 = Meta_L
add Mod1 = Meta_L
add Mod2 = Mode_switch

It clears the keymappings for alt and escape and then reassigns them to the reverse of what they were before. Restart X11 and it should pick up the changes.

If this was (or wasn’t) helpful, please let me know in the comments!

25 Responses to “Redefine the X11 Meta Key in Mac OS X”


  1. 1 Hang Si

    It works, amazing! I’m really happy to find the artile. I’ve just migrated from Linux to MacOSX. Emacs is my all-day working editors. I was quite frustrating at the beginning that I found the meta key didn’t work. I tryed very hard to activate it by both combining different keys and changing the setting in .emacs file, but all effort was in vain. Then I came to your article after searching in the google for a while. Now I can keep working on my favorite editor. Thank you!

  2. 2 Nick

    Thanks that worked great for me too. I found you blog via Google also.

  3. 3 keycoffee

    Weyell - it works fine for me, if I’m just working on my local machine. This is indeed fabulous. But - when I try and log into a separate linux server this trick doesn’t work (sigh) - even with a .Xmodmap file in my home directory on the other machine.

    Its very frustrating - emacs is soo important to my work and keyboard happiness is crucial.

  4. 4 Oskar

    Thanks, it worked great! While we’re at it, how to copy/paste between OS X and X11 (and emacs in particular)? :)

  5. 5 Tyler

    I can copy from OS X to emacs using the standard Command-c in OSX then Ctrl-y in emacs.

    To go the other way, use Command-c in X11 and Command-v in your favorite OS X application.

    Hope that helps!

  6. 6 Paolo

    Works great! Thanks for the tip, Tyler!

  7. 7 Peter

    As far as it working on other machines, I have in my local ~/.Xdefaults file the following lines:

    XTerm*VT100.metaSendsEscape: True
    XTerm*VT100.backarrowKeyIsErase: False

    This is (I think) what finally made it possible for me to get meta on remote machines working. Of course this only fixes the problem for xterm. Other terminal programs are still broken (and I’m afraid the OSX Terminal.app is hopeless.)

    I pretty much live in Emacs, and I’m trying very hard to be happy with OSX. But the pain of using this editor with OSX, and the uncertainty that DoubleCommand will continue to work with future verions of OSX makes me second guess the wisdom of buying a Mac. After all, a mondern user-oriented OS shouldn’t force me to abandon 20 years of burnt-in muscle memory, should it?

  8. 8 Roger

    Peter — your .Xdefaults modification works wonders. Thank you for sharing it!

  9. 9 David Brodbeck

    Wow, great article. I now have some consistency between my xterms and Terminal. I had previously checked ‘Use Option Key As Meta Key’ in the Terminal window settings to get the behavior I wanted there, but my xterms were still using Esc as meta until I found this article in Google.

    I never really understood why anyone would use emacs until I got a proper Meta key working. Without that it’s just plain tedious.

  10. 10 yashar

    awesome. thanks.

  11. 11 kourouma

    Hi there. I would like to create a Xmodmap file as well, but unfortunately I have not the slightest idea how to do that. Can somebody please tell me? the home folder your talking about is the one with house icon I take it?
    Thanks
    Kourouma

  12. 12 Tyler

    Yes, the home folder for your user account is the one with the house icon. If your username is foo, then the path of this folder is /Users/foo. Another way of referencing the home folder is by using the ~ character, which expands to /Users/foo.

    To create the file, either use TextEdit (or your favorite text editor), or use emacs or vi in the Terminal.

  13. 13 kourouma

    Thanks

  14. 14 Ian

    Thanks for this nice discussion and tips. I just got a new Mac, and when I followed your instructions it seemed to switch the alt/option key with the Command/Apple key, rather than switching the alt/option key with the esc key. Could this be because of my version of X11 (1.1.3), or because I did something wrong, …? Weird that it seemed to work for everyone else. After a lot of blind attempts and googling, though, I finally got alt/option and esc to be switched on my computer: now alt/option is the emacs meta key (both on local emacs in the X11 terminal window and when I ssh to an emacs session on a remote Linux machine). In case someone else runs into this same problem, here’s what I put in my .Xmodmap file (just these 2 lines):

    keysym Mode_switch = Escape
    keysym Escape = Mode_switch

  15. 15 Eugene

    I’m freaking out cause this makes NO sense
    When I run xmodmap (for Tiger OS X), the mods are listed as:

    mod1 Meta_L (0×42)
    mod2 Mode_switch (0×3f), Mode_switch (0×45)

    Why Are there 2 Mode_switches? I thought I was only assigning Escape to Mode_switch… but Apple Command is not ‘copying’ the way it did in Panther. Apple Command-c gives me a modified character ‘c’ with the little squiggly line coming underneath (like the French word facade)… WTF?!?!?!?!? I want Apple Command to do what it is supposed to do by default! No Mode_switch crap… I don’t even have an .xinitrc file… only a .Xmodmap file. It has exactly the 6 lines from this page. Why the hell are there two Mode_switch bindings???

  16. 16 Eugene

    One more time… My .Xmodmap:
    clear mod1
    clear mod2
    …. then I run xmodmap and I get:
    shift Shift_L (0×40), Shift_R (0×44)
    lock Caps_Lock (0×41)
    control Control_L (0×43), Control_R (0×46)
    with the mod’s being EMPTY.

    Now I put .Xmodmap to be:
    clear mod1
    clear mod2
    keycode 66 = Meta_L
    add mod1 = Meta_L

    This gives me an output from xmodmap:
    shift Shift_L (0×40), Shift_R (0×44)
    lock Caps_Lock (0×41)
    control Control_L (0×43), Control_R (0×46)
    mod1 Meta_L (0×3f), Meta_L (0×42)

    WHY THE HELL ARE TWO META_L’s BEING ASSIGNED??????????!!!!!!!!!!
    I’m gonna throw this freakin laptop out the window.

  17. 17 Tom

    I’m confused … why did you need to do this?

    X11 on Mac OS X defaults to having the meta key be the command key (thank god. My hands are programmed for that key at that position from years of X11 on Linux / BSD)

    What does this remapping accomplish??? Does it make the option/alt key the meta key? eeew.

    > To go the other way, use Command-c in X11 and Command-v in
    > your favorite OS X application.

    That doesn’t work for me….

  18. 18 Steven G. Harms

    Excellent post.

    This helped me immeasurably. I am now able to move through the bash shell as god intended ( with emacs bindings ). I don’t use emacs anymore though, it upset my carpal tunnel too much ( vi dvorak keyboard or Textmate dvorak has proved to be my savior ).

    Thanks again though,

    Steven

  19. 19 William Daffer

    > To go the other way, use Command-c in X11 and Command-v in
    > your favorite OS X application.

    This didn’t work for me either.

    Just to be completely explicit, I’d like a key binding in emacs that would put the copied region into the Mac clipboard so that I could then use Command-V in some non-X11 application to paste that text in.

    Is this possible?

  20. 20 Dancing with Penguins

    You don’t need to restart X11. Just run

    xmodmap ~/.Xmodmap

    should be sufficient. That way you can try out different settings without restarts.
    You should start with a

    xmodmap -pke > ~/.Xmodmap.default

    however, as this gives you the possibility to recover at any point by

    xmodmap ~/.Xmodmap.default

    to your initial keyboard mapping.

    Peter

  21. 21 Lance E Sloan

    It didn’t work with GIMP. That is, after the change, I expected to use the command key with the underlined letters in the UI in order to activate them, and that didn’t work. I thought that command-F would open the file menu, etc. But it doesn’t, it still requires option-F instead.

  22. 22 Tyler

    @Lance - That’s not the problem that this article is trying to solve. But to comment on your issue, a native port of Gtk+ for Mac OS X is currently being developed. Gtk+ is a graphics/windowing toolkit which is standard fare on Unix/Linux systems. When the port is completed, apps like Gimp will be able to be run on Macs without using X11. One of the features of this port is that ctrl+ shortcuts will be remapped to command+ shortcuts.

    See the Gtk+ for Mac OS X Development Page for more details.

  23. 23 Randy Bushq

    i LIKE meta being apple key. but why is it not working in emacs? lack of meta in emacs is killing me!

    [ long time bsd/emacs user with new macbook ]

  24. 24 Denali Lumma

    Hi There,

    You need to configure the mac ‘terminal’ application so the ‘alt / option’ key sends ‘meta’. In order to do this, go to:

    Terminal > Preferences > Settings > Keyboard

    and select the ‘Use option as meta key’ checkbox at the bottom of the window.

    I hope that helps,

    -Denali

  1. 1 the jakbot - Redefine the X11 Meta Key in Mac OS X

Leave a Reply