How to alias keybindings in emacs?

0

2

For instance, I get to use C-c a a a lot, and would like to easily map this to f1.

I know I can search for what function the first binding finaly calls (this time it was org-agenda-list), but I'm after a more general solution. I don't think I can use (global-set-key KEY COMMAND), for I'm not sure if pressing keys is a COMMAND in itself (but maybe it can be?)

So how to bind "multiple successive keypresses" to another binding?

Nikana Reklawyks

Posted 2012-11-02T16:58:04.060

Reputation: 501

Answers

2

The code you are looking for is:

(define-key keymap-1 (kbd "new prefix")
 (lookup-key keymap-2 (kbd "old prefix")))

You don't usually need to know the names of the keymaps as the expressions (current-global-map) and (current-local-map) return the global and local maps in force. Although the emacs manual says you can re-map any of the function keys, I have found that strange things can happen if you try to re-map f1, f2, f3, f4 or f10. For this reason I usually re-map f5 or f6. As an example from my .emacs file, the code:

(add-hook 'planner-mode-hook
    #'(lambda ()
        (local-set-key (kbd "M-RET") 'muse-insert-list-item)
        (local-set-key (kbd "M-S-RET") 'pcomplete)
        (define-key (current-local-map) (kbd "<f5>")
          (lookup-key (current-local-map) (kbd "C-c C-j")))))

sets M-RET and M-S-RET in planner-mode and also makes the prefix f5 into an alias for C-c C-j in that mode.

Be aware, however, that it doesn't always work as lookup-key seem understand macros which, unfortunately, are legal in keymaps. In such cases you can usually find a way round it. Ask again if you have problems.

Bernard Hurley

Posted 2012-11-02T16:58:04.060

Reputation: 36

I'm not sure why you're mentionning prefixes, but this works, and the hook idea is interesting (even though i'm more familiar with knowing how keymaps are called). I donk know much about macros, the bright side being I therefore don't care (I mean, if that's the C-x ( / C-x ) thing, mapping constructs based on them to other bindings seems very deviant to me >_<). Are you running in problems with f1 to f4 and f10 because they're already bound, sometimes later defined than your own definition, by any chance ? – Nikana Reklawyks – 2012-11-20T07:08:35.647

Some major modes rebind them. For instance f1 is bound to help but some major modes have their own variant of help and re-bind the key. I find it easier not to use them. – Bernard Hurley – 2012-11-20T08:33:05.247

-1

I think, but didn't try, that you should do something like this:

(defun org-execute-my-action ()
  (local-set-key (kbd "C-c q") 'org-name-of-function))

(add-hook 'org-mode-hook 'org-execute-my-action)

This post seems to be the place for further details.

Dror

Posted 2012-11-02T16:58:04.060

Reputation: 1 510

Err, I don't see how that's adressing my question… That's merely binding a key locally in the org-mode-map, right? I'd like to bind a one key to "multiple keypresses". (For instance, there is no place for f1 in you example.) – Nikana Reklawyks – 2012-11-04T20:58:43.130