43

I have globally assigned C-c/ to ace-jump-mode but reftex-mode (a minor mode for citations used with AucTeX) overrides this key with some function I never use.

I tried local-unset-key but it only unbinds keys from the current major mode's map.

How do I remove C-c/ from reftex-mode-map without making changes to reftex.el?

malana
  • 5,045
  • 3
  • 28
  • 41

3 Answers3

57

You can change an existing key map using define-key. By passing nil as the function to call, the key will become unbound. I guess that you should be able to do something like:

(define-key reftex-mode-map "\C-c/" nil)

Of course, you should do this in some kind of hook, for example:

(defun my-reftex-hook ()
  (define-key reftex-mode-map "\C-c/" nil))
(add-hook 'reftex-mode-hook 'my-reftex-hook)
Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • 23
    No. Don't call define-key from hooks except for very specific modes that aggressively enforce its own binding. I actually can't think of any examples. Each minor/major mode only has one keymap, so there is no point in repeatedly modifying it every time you enter the mode. Use `eval-after-load` instead. – event_jr Sep 29 '11 at 14:30
  • Thanks to both. It didn't occur to me to use nil to undefine a key. The eval-after-load tip completes the answer. I'll accept the answer and +1 the comment. – malana Sep 29 '11 at 15:33
  • 2
    To be clear, @event_jr is suggesting something like `(eval-after-load 'reftex '(define-key reftex-mode-map "\C-c/" nil))`. The first argument to `eval-after-load` -- here `reftex` -- is the name that the mode "provides". – ntc2 Oct 20 '15 at 01:51
  • 1
    in response to event_jr, here's an example of aggressively enforcing a mode's own binding: flyspell-mode. Occasionally it overwrites my key bindings even though I used eval-after-load to set it to null and then set it in my mode to what I want. – Adrian Chira Nov 19 '15 at 17:42
  • `(define-key reftex-mode-map "\C-c/" nil :append t)` Append in case the binding itself is defined in a hook. – Michael Fox Mar 11 '17 at 00:15
7

You can use following command:

(define-key reftex-mode-map "\C-c/" nil)

to unmap this function from C-c /... But reftex-mode should be loaded, so reftex-mode-map will available for modification

Alex Ott
  • 80,552
  • 8
  • 87
  • 132
1

This is how I do it. It could be improved, though.

 (defun get-key-combo (key)
  "Just return the key combo entered by the user"
  (interactive "kKey combo: ")
  key)

(defun keymap-unset-key (key keymap)
    "Remove binding of KEY in a keymap
    KEY is a string or vector representing a sequence of keystrokes."
    (interactive
     (list (call-interactively #'get-key-combo)
           (completing-read "Which map: " minor-mode-map-alist nil t)))
    (let ((map (rest (assoc (intern keymap) minor-mode-map-alist))))
      (when map
        (define-key map key nil)
        (message  "%s unbound for %s" key keymap))))
;;
;; Then use it interativly
;; Or like this:

(keymap-unset-key  '[C-M-left]   "paredit-mode")
..
..
knobo
  • 86
  • 8
  • This is mostly useful for interactively use. I usually do that to find out which map to remove the key from, and the name of the key. It is easier then looking up in the source-code to find the map, and to know the **name of the key** to unbind. For example Ctrl-| is the key [67108988]. Then I use repeat-complex-command and cut and paste it in to .emacs. – knobo Apr 22 '16 at 07:27