Emacs Unbind a Mode's KeyBinding [duplicate]
Asked Answered
A

2

10

I've created a custom key binding macro as follows:

(global-set-key (kbd "C-C C-c") "\C-a\C- \C-n\M-w\C-y")

The problem is that C-c C-c is defined for python-send-buffer in python-mode. So my macro works for all modes except python-mode. I am assuming that python-mode is evaluated after my init file, so it overwrites that keybinding.

I tried unsetting C-c C-c using (eval-after-load "python-mode") and using global-unset-key but that doesn't work. C-c C-c in python is always mapping to python-send-buffer.

How can I completely disable Python's C-c C-c, and use my macro instead?

I am using Emacs 24.2.1.

Adrastus answered 11/10, 2013 at 18:11 Comment(5)
C-c C-c is defined for a lot of modes. Maybe you should consider #683925Peculium
Or just consider using a different key. C-c followed by a control key (such as C-c) is "reserved for major modes", meaning that Lisp code that defines a major mode is entitled to use it (and they often do use C-c C-c, as ScottFrazer said). Users are still entitled to bind such a key, of course, but just know that major modes will feel free to do so also, so you might need to then find a replacement key to use for such modes (see the answers here for that). See the Elisp manual, node Key Binding Conventions.Anorak
@Anorak I can't seem to find a C-x binding that is free and easy to access/makes sense for doing this copy & paste which I use very often.Adrastus
This question is a not a duplicate of https://mcmap.net/q/436924/-unset-key-binding-in-emacs since the answers over there do not suggest using add-hook, but the OP states that they used add-hook and it did not work. Marking this question as a duplicate of that question is misleading. The answers for that question do not work in my case, which is why I posted this question.Adrastus
@Darsky: Then don't use prefix C-x for this. There are still plenty of keys available to you. See the manual, Key Binding Conventions for which keys are reserved for users. (And you can also bind any other keys, but they might override mode keys etc.) And you can define your own prefix keys: e.g., you can make <f8> be a prefix for some or all of your own keys.Anorak
D
11
(add-hook 'python-mode-hook
          (lambda()
            (local-unset-key (kbd "C-c C-c"))))
Dichroism answered 11/10, 2013 at 18:25 Comment(0)
C
3

This should do it:

(add-hook 'python-mode-hook
          (lambda()
            (define-key python-mode-map (kbd "C-c C-c") nil)))
Concettaconcettina answered 11/10, 2013 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.