1

Suppose I rebind C-e (move-end-of-line) to C-o in the global-map.

How can I make C-o the corresponding function in all modes (if it exists).

So, for example, in org-mode C-o should be org-end-of-line, not move-end-of-line.

NB: I do not simply want, in this example, C-o to be move-end-of-line everywhere. Rather I want it to be the corresponding command for that mode, which in this case is org-end-of-line.

cammil
  • 509
  • 3
  • 12
  • I do not know if there is an "emacs.stackexchange.com" answer, but there are some answers on stackoverflow with the Emacs tag: https://stackoverflow.com/questions/683425/globally-override-key-binding-in-emacs In addition to the answers in that linked thread, I've seen at least one other variation of an answer by @phils that provides a minor-mode definition ... [I didn't see it in my limited Google search today, but have seen it in the past.] – lawlist May 06 '20 at 18:21
  • @lawlist: Maybe [this](https://stackoverflow.com/questions/683425/globally-override-key-binding-in-emacs/5340797#5340797)? – NickD May 06 '20 at 19:04
  • @NickD -- thanks for looking for additional threads where *phils* has posted some solutions. The link you cited is one of the multiple answers to the same thread that I linked in the comment above yours. I just did some more Googling and can't seem to find the thread I remembered reading ... probably on stackoverflow from a few years ago. – lawlist May 06 '20 at 19:52
  • Yes, sorry - I looked after I posted and it did not conform to your description. – NickD May 06 '20 at 19:56
  • Does this answer your question? [How to override major mode bindings](https://emacs.stackexchange.com/questions/352/how-to-override-major-mode-bindings) – lawlist May 06 '20 at 19:58
  • It doesn't seem to. If I use `bind-keys*` the other modes do not seem to be affected. – cammil May 06 '20 at 21:36
  • Note that all `bind-keys*` does is make sure that the keybinding is not overridden. That is not what I am asking. See the question for full info. – cammil May 06 '20 at 21:49

1 Answers1

2

Not sure I understand your question. Are you asking how, in each major mode, to bind to C-o whatever that mode normally binds to C-e?

If so, then in after-change-major-mode-hook add a function such as this:

(defun my-remap-C-e-to-C-o ()
  "Remap whatever command is locally bound to `C-e` to `C-o`."
  (let* ((map       (current-local-map))
         (C-e-bind  (lookup-key map (kbd "C-e") t)))
    (define-key map (kbd "C-o") (or C-e-bind  'move-end-of-line))))

(add-hook 'after-change-major-mode-hook #'my-remap-C-e-to-C-o)

If the local mode does not, itself, bind C-e then the C-o gets bound, in that mode, to move-end-of-line.

This code does not remove the global binding of C-e. If you also want to do that then:

(global-unset-key (kbd "C-e"))
Drew
  • 75,699
  • 9
  • 109
  • 225