27

I want to get rid of the C-e binding so I can bind C-e e, and C-e r to a command. How do I do this?

My init file has this:

    (global-set-key (kbd "C-e e") 'move-end-of-line)
    (global-set-key (kbd "C-e r") 'end-of-buffer)
6
  • Here is what I do for C-d -- perhaps it would work for you using C-e instead? -- the help-for-help function is just an arbitrary example: (defalias 'ctl-d-keymap (make-sparse-keymap)) (defvar ctl-d-map (symbol-function 'ctl-d-keymap) "Global keymap for characters following C-d.") (define-key global-map "\C-d" 'ctl-d-keymap) (define-key ctl-d-map "z" 'help-for-help)
    – lawlist
    Commented May 13, 2015 at 19:06
  • Sadly didn't work :( but thanks anyway! Commented May 13, 2015 at 19:15
  • 3
    What do you get when you do C-h k C-e? Commented May 13, 2015 at 19:50
  • If you have a major-mode or a minor-mode that is active and uses C-e, then those key bindings will trump a global setting -- minor trumps major, and major trumps global. So, you would need to do what kaushalmodi is suggesting to track down which of those circumstances is likely at issue. Once you figure that out, then you can unset those keys. Here is an example for unsetting org-mode key bindings (a major mode): stackoverflow.com/a/17540326/2112489
    – lawlist
    Commented May 13, 2015 at 20:02
  • If, for example, you have a stock Emacs installation (built for OSX) with zero user configuration, the answer to kaushalmodi's question is: "C-e runs the command move-end-of-line (found in global-map), which is an interactive compiled Lisp function in 'simple.el'. It is bound to C-e. (move-end-of-line ARG)" Absent you providing an answer to that specific question, everyone here is just going to be guessing what the conflict might be. And, of course, it is always a good idea to restart Emacs when adding/modifying settings so that those new settings can take effect.
    – lawlist
    Commented May 13, 2015 at 22:42

3 Answers 3

33

You can unset the key in at least two ways:

(global-set-key (kbd "C-e") nil)
(global-unset-key (kbd "C-e"))

Note that I got this information with a web search for emacs unset key.

7
  • Neither worked :( Commented May 13, 2015 at 19:23
  • 2
    @JoshuaLilleberg: you're going to have to provide more information before we can help.
    – Dan
    Commented May 13, 2015 at 19:24
  • What do you mean more info? Do you want to know what I'm running? What I put in my .emacs? I dont understand... Commented May 13, 2015 at 21:44
  • Thanks. This also worked for combinations like (global-unset-key (kbd "C-h h")).
    – MattMS
    Commented Oct 16, 2018 at 4:13
  • 1
    The second one, global-unset-key worked for me.
    – emonigma
    Commented Dec 6, 2018 at 10:56
7

See @lawlist's comment.

Use C-h m, C-h k, and C-h b wherever you want to make the change. That will help you figure out the keymap in which you need to make the change. If Dan's suggestion didn't help then clearly that keymap is not the global map.

Setting the key binding to nil is indeed the way to unbind it. You just need to do that in the right map.

Using C-h M-k (describe-keymap), from library help-fns+.el will give you a human-readable list of the bindings in a given keymap (bound to a keymap variable, such as global-map.

5
  • I looked through the C-h b place, I found C-e , it was in the Global Bindings category, I don't know what to do know. Commented May 13, 2015 at 22:17
  • What does C-h k tell you in the buffer in question? If it says something like C-e runs the command move-end-of-line (found in global-map) then the binding is from the global map, and (global-set-key (kbd "C-e") nil) should be sufficient. If it is not then you will need to show us more context. And be sure to start from emacs -Q (no init file) - we won't try to guess whatever might be in your init file and setup.
    – Drew
    Commented May 13, 2015 at 22:44
  • All it says is: C-e runs the command move-end-of-line, which is an interactive compiled Lisp function. It is bound to C-e, <end>. (move-end-of-line ARG) Move point to end of current line as displayed. With argument ARG not nil or 1, move forward ARG - 1 lines first. If point reaches the beginning or end of buffer, it stops there. To ignore the effects of the intangible' text or overlay property, bind inhibit-point-motion-hooks' to t. If there is an image in the current line, this function disregards newlines that are part of the text on which the image rests. Commented May 14, 2015 at 19:40
  • So Emacs says that C-e is bound to move-end-of-line. And you try M-x global-unset-key C-e and try C-h k C-e and it still tells you that it is bound to move-end-of-line. Right? Try doing that same thing after starting Emacs using emacs -Q (no init file). No doubt it will work. If so, and if it doesn't work when you load your init file (just emacs, not emacs -Q), then something in your init file is causing the problem. Recursively bisect your init file to find what that something is. You can comment out 1/2, then 3/4, 7/8 etc. of your init file using command comment-region.
    – Drew
    Commented May 14, 2015 at 20:07
  • This answer would be complete with an example for unsetting in a different map
    – mcp
    Commented Jun 5, 2021 at 14:46
4

You don't need to unset a key before you rebind it to something else. This should do what you want:

;; create a new prefix map
(define-prefix-command 'my-keymap)
;; bind the new keymap to C-e 
(global-set-key "\C-e" my-keymap)
;; bind the individual commands:
(define-key my-keymap "e" 'move-end-of-line)
(define-key my-keymap "r" 'end-of-buffer)

Now hitting C-e is a prefix, and C-e e calls end-of-line etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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