12

Using Gnu Emacs in MacOS "El Capitan".

Question:

How do I bind the right option key to control but keep the left option key bound to option?

Background:

I found that even with caps_lock as control my left hand would start to ache, so I decided to have as many control keys as possible. Before switching to Mac I never used caps_lock or option (still don't use caps_lock), so I bound both of them to control: caps_lock to control in the Keyboard systems preferences and option to control in my .emacs file.

Unlike PC keyboards, the Apple keyboard has only one control key but two option keys (one on the outside of each of the two command keys):

enter image description here

But now I'm finding that I would like to be able to have an option key in Emacs. There is already a control key on the bottom left but not one on the bottom right, so ideally I would revert just the left option key (now mapped to control) back to option But I still want the right option key to be a control.

These are the relevant lines from my .emacs file.

(setq 
  mac-option-key-is-control t
  mac-option-modifier 'control
 )
2
  • If you need any special symbols or type in a foreign language somtimes, you may wish to keep the right option key available for OSX default behavior: (setq ns-right-alternate-modifier 'none) and set the ns-right-command-modifier to whatever you want -- e.g., 'control.
    – lawlist
    Jul 28, 2016 at 16:40
  • Also: some versions of Emacs for OS X use mac-right-option-modifier instead of ns-right-option-modifier.
    – amitp
    Jul 28, 2016 at 17:34

3 Answers 3

11

You're almost there.

By default, the ns-XXXXX-modifier variables affect both the left and right versions of the key. However, you can change the right one with ns-right-XXXXX-modifier.

So: to set the left one to A and the right one to B, you can set the ns-XXXXX-modifier to A and ns-right-XXXXX-modifier to B.

The following example sets the left option modifier to meta, and the right one to control:

(setq ns-option-modifier      'meta
      ns-right-option-modifer 'control)

See https://emacsformacosx.com/tips ("Changing the Modifier Keys") for more details.

3
  • Perfect. Thanks! Now since I want to keep left option as option and change just right option to control, I just need to do (setq ns-right-option-modifer 'control). But I still don't know what "ns" means. I have "mac" instead of "ns" in my other setq commands.
    – Ruby
    Jul 28, 2016 at 22:17
  • 1
    @Ruby: the ns, I believe, refers to the NeXTSTEP operating system that became the source for OSX after Apple bought it. My best guess is that you can use the ns and mac prefixes interchangeably on these variables, but I haven't checked.
    – Dan
    Jul 28, 2016 at 22:26
  • Indeed, the mac- version is just an alias for the ns- version of these symbols.
    – tripleee
    Apr 20, 2021 at 5:15
4

Replying to Dan's answer:

The behavior of the mentioned variables have changed (at least in Spacemacs): ns-right-option-modifer triggers ns-option-modifier as well - which makes no sense if you ask me - but if you set:

ns-option-modifier to meta

and

ns-right-option-modifer to control (just for this example)

pressing just the right option key will result in "C-M-x" instead of "C-x".

The solution is to switch the keys and use left option key for accented characters.

Check the details here: https://github.com/syl20bnr/spacemacs/issues/10499

Solution:

(setq ns-option-modifier 'none
      ns-right-alternate-modifier 'meta)

Further comments:

(defvaralias 'mac-allow-anti-aliasing 'ns-antialias-text)
(defvaralias 'mac-command-modifier 'ns-command-modifier)
(defvaralias 'mac-right-command-modifier 'ns-right-command-modifier)
(defvaralias 'mac-control-modifier 'ns-control-modifier)
(defvaralias 'mac-right-control-modifier 'ns-right-control-modifier)
(defvaralias 'mac-option-modifier 'ns-option-modifier)
(defvaralias 'mac-right-option-modifier 'ns-right-option-modifier)
(defvaralias 'mac-function-modifier 'ns-function-modifier)

(defvaralias 'ns-option-modifier 'ns-alternate-modifier)
(defvaralias 'ns-right-option-modifier 'ns-right-alternate-modifier)

Emacs version: [email protected] (spacemacs-base)

1

emacs on osx has a few ns-*-modifier variables that help for this particular case.

in general, you can use karabiner (https://pqrs.org/osx/karabiner/) to remap keys under osx.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.