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.

Drew
  • 29,895
  • 7
  • 74
  • 104
darksky
  • 20,411
  • 61
  • 165
  • 254
  • C-c C-c is defined for a lot of modes. Maybe you should consider http://stackoverflow.com/questions/683425/globally-override-key-binding-in-emacs – scottfrazer Oct 11 '13 at 20:12
  • 1
    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`. – Drew Oct 11 '13 at 22:07
  • @Drew 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. – darksky Oct 14 '13 at 16:28
  • This question is a not a duplicate of http://stackoverflow.com/questions/13965966/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. – darksky Oct 14 '13 at 16:39
  • @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 `` be a prefix for some or all of your own keys. – Drew Oct 14 '13 at 18:00

2 Answers2

11
(add-hook 'python-mode-hook
          (lambda()
            (local-unset-key (kbd "C-c C-c"))))
Andreas Röhler
  • 4,804
  • 14
  • 18
3

This should do it:

(add-hook 'python-mode-hook
          (lambda()
            (define-key python-mode-map (kbd "C-c C-c") nil)))
abo-abo
  • 20,038
  • 3
  • 50
  • 71