Question

I want to use Undo Tree Mode, and I have the undo/redo key bindings set up successfully.

However, when I try to overwrite the default C-/ binding with a commenting function I wrote, the combination is still bound to undo-tree-undo.

Here is what I tried:

(eval-after-load 'my-functions
  '(eval-after-load 'undo-tree
    '(define-key global-map (kbd "C-/") 'my-commenting-function)))

I have it load after my-functions so my-commenting-function can be called.

After trying this, with Undo Tree Mode enabled, C-/ is still bound to undo-tree-undo and not my-commenting-function.

How can I change the Undo Tree Mode key binding for C-/ to the function I want?

Was it helpful?

Solution

Do this to define a global binding:

(global-set-key (kbd "C-/") 'my-commenting-function)

Do this to stop a minor mode from overriding it:

(define-key undo-tree-map (kbd "C-/") nil) 

OTHER TIPS

As @abo-abo has alluded to the problem is the order in which key-bindings are evaluated. If you read the Emacs documentation (http://www.gnu.org/software/emacs/manual/html_node/elisp/Active-Keymaps.html#Active-Keymaps) you will see the keymaps are searched in order:

keymap, minor mode maps, local keymap, global keymap

There are two approaches to avoiding the sort of aliasing you describe. Either modify any minor mode keymaps that clash or set the buffer keymap directly (say with a hook function). Also there is a convention that C-c 'letter' is reserved for user mappings and in theory shouldn't be overridden by any other modes so you could consider using one of those bindings instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top