0

Can one use macOS's Command Key (⌘) as a separate modifier key from ctrl and meta?

My specific question is, is it possible to use ⌘+Q to quit emacs without altering the existing keybindings? I have something like this in mind:

(bind-key "\⌘-q" 'save-buffers-kill-terminal)

This particular code doesn't work, of course.

I've been using emacs longer than I've been using macOS and don't want a whole-sale change like that mac-key-mode offers. I just want a selected set of mac shortcuts to work on emacs.

The version of emacs I uses is the one from the emacs-mac cask package of homebrew. The package description includes

railwaycat/emacsmacport/emacs-mac: stable emacs-27.2-mac-8.3, HEAD
YAMAMOTO Mitsuharu's Mac port of GNU Emacs
https://www.gnu.org/software/emacs/

Edit: I've started wonder whether my question is the same as

Carbon Emacs ⌘-shortcuts in Mituharu-Emacs?

If so, the only solution seems to be for me to use a different emacs package than emacs-mac . . . .

Ryo
  • 125
  • 5
  • Which Emacs are you using? By default the Cmd key is set to act as `super` and Cmd+q is bound to `ns-power-off`, which quits Emacs.app. – d125q Feb 23 '22 at 08:01
  • @d125q Thanks! but I don't find the `ns-` functions on my emacs. I looked it up in `M-x describe-function` and `M-x describe-variable` but no functions of variables start with a `ns-` prefix. What does that mean? – Ryo Feb 23 '22 at 16:59

1 Answers1

3

By default ns-command-modifier is super. So you can use (bind-key "s-q" 'save-buffers-kill-terminal)

Update: As you said you are using emacs-mac, and the default value of mac-command-modifier is meta. I suggest to change mac-command-modifier to super or hyper (i.e. (setq mac-command-modifier 'super)) and use mac-option-modifier as meta.

Besides, the corresponding prefixes and modifiers are: M- (meta), C- (control), S- (shift), A- (alt), H- (hyper), and s- (super). For more details, please see the doc of bind-key and edmacro-mode.

Tianshu Wang
  • 1,724
  • 4
  • 7
  • Thanks! but I don't find the `ns-` functions on my emacs. I looked it up in `M-x describe-function` and `M-x describe-variable` but no functions of variables start with a `ns-` prefix. What does that mean? – Ryo Feb 23 '22 at 17:00
  • are you using emacs-mac? what about `mac-command-modifier` – Tianshu Wang Feb 24 '22 at 00:26
  • Yes, I am using emacs-mac . mac-command-modifier is currently set to `meta`. By the way, `(bind-key "s-q" 'save-buffer-kill-terminal)` doesn't seem to make any changes. – Ryo Feb 25 '22 at 04:08
  • 1
    Yes becasuse the value is `meta` not `super`. – Tianshu Wang Feb 25 '22 at 06:57
  • 1
    @Ryo I have update the modifiers and prefixes in the answers. I suggest to change `mac-command-modifier` to `super` or `hyper`(i.e. `(setq mac-command-modifier 'super)`) and use `mac-option-modifier` as `meta`. – Tianshu Wang Feb 25 '22 at 07:01
  • Thanks! Adding `(setq mac-command-modifier 'super)` and `(bind-key "s-q" 'save-buffers-kill-terminal)` has worked! Could you add `(setq mac-command-modifier 'super)` to your answer above to help future visitors to stackexchange? – Ryo Feb 25 '22 at 08:22
  • 1
    Hi @Ryo, added it. – Tianshu Wang Feb 25 '22 at 14:27