Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set CMD key-binding in Emacs?

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)
like image 666
cannyboy Avatar asked Feb 15 '13 23:02

cannyboy


People also ask

How do I bind a key in Emacs?

To interactively bind keys for all modes, type M-x global-set-key RET key cmd RET . To bind a key just in the current major mode, type M-x local-set-key RET key cmd RET . See Key Bindings in The GNU Emacs Manual .

What is a prefix key in Emacs?

Prefix keys are keys like C-x and C-c; they are parts of a complete key, and each constituent part of a prefix key is made up of its own keymap. A complete key is a command that, when input, executes its associated command. It is possible for Emacs to enumerate all the active minor and major mode key bindings in a buffer by typing C-h m.

How do I replace a key with another key in Emacs?

You can tell Emacs that you want to replace all keys pointing to a certain command with one of your own choosing by using the remap event; this should be done instead of passing a key to the key bind function you are using.

What does a complete key do in Emacs?

A complete key is a command that, when input, executes its associated command. It is possible for Emacs to enumerate all the active minor and major mode key bindings in a buffer by typing C-h m. This command is very useful if you want to learn more about what a major or minor mode can do.


Video Answer


2 Answers

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)
like image 123
Nico Avatar answered Sep 24 '22 20:09

Nico


In Aquamacs, what worked for me is the following

(global-set-key (kbd "A-r") 'my-command)
like image 39
magicrebirth Avatar answered Sep 26 '22 20:09

magicrebirth