Macos – How to set CMD key-binding in Emacs

emacshomebrewmacos

I'm using Emacs in Mac OS X terminal, installed with homebrew.

My CTRL key is my C key, and the ALT key is Meta.

How would I define key-bindings for CMD key combinations?

For instance, I want to set CMD-(right arrow) to go to the end of the line.


EDIT

I've tried @nickcarlo's suggestions below

(setq mac-command-modifier 'super)
(global-set-key (kbd "s-<right>") 'move-end-of-line)

I don't think CMD key is being set to super properly, since I don't see s-foo in the mini-buffer as I would if I had typed C-x or M-x or anything. I noticed that CMD-right, when I have two terminal windows open, switches between the two terminal windows, so I thought that might be blocking any custom setting. However, I tried:

(global-set-key (kbd "s-9") 'move-end-of-line) 

.. and CMD-9 still does nothing, except beep to tell me I've pressed something wrong.

Setting non-CMD key-combos seems to work fine, like:

(global-set-key (kbd "C-w") 'move-end-of-line)

Best Answer

You can set it with something like:

(global-set-key (kbd "s-<right>") 'move-end-of-line)

"s" being the reference to your command key.

PS: I'm not on my mac right now so I'm not sure what exactly you need to write to set it to the specific keys you mentioned to move it to the end of line but command is represented by an s in emacs.

EDIT Just tested this on my mac. This is the exact code you'd write (if you wanted to set it globally) to bind your command key + right arrow key to move your cursor to the end of line.

If the above doesn't work, you could also try

(global-set-key (kbd "\s <right>") 'move-end-of-line)

The difference between the one above and this one is that the above equates to CMD+right and this one equates to CMD and then release CMD and hit the right key. Both work on OS X on my system.

Also check out the following post to set your CMD as the super key in case your Emacs doesn't already register it as such:

http://ergoemacs.org/emacs/emacs_hyper_super_keys.html

Specifically, you can use the following code to set up your CMD key as Super:

(setq mac-command-modifier 'super)