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.

  • I do not know if there is an "emacs.stackexchange.com" answer, but there are some answers on stackoverflow with the Emacs tag: stackoverflow.com/questions/683425/… 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 6 at 18:21
  • @lawlist: Maybe this? – NickD May 6 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 6 at 19:52
  • Yes, sorry - I looked after I posted and it did not conform to your description. – NickD May 6 at 19:56
  • Does this answer your question? How to override major mode bindings – lawlist May 6 at 19:58
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"))
| improve this answer | |
  • Yep. You understand my question. – cammil May 7 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.