How to override a key combination?

31 views
Skip to first unread message

Derek Graham

unread,
Sep 10, 2015, 11:56:33 AM9/10/15
to Emacs Prelude
Hello,

In my .emacs.d/personal I have a file with my preferred key bindings, however, they do not override the prelude mappings. E.g. In that file, I have

(global-set-key (kbd "C-c g") 'goto-line)

In what order are the files in personal read? Perhaps not last, so then I've also eval'd that line in emacs, but still it's not overridden. I've tried this before the above line too:

(local-unset-key (kbd "C-c g"))

But still doesn't work. What is the recommend method?

Emacs version: 24.5.1
New install of prelude two days ago (not sure how to get the version)
The *Messages* buffer returns goto-line when I eval the first line of code, but nothing else.

Many thanks,

Derek


Derek Graham

unread,
Jan 5, 2016, 8:02:43 AM1/5/16
to Emacs Prelude
For anyone else, this isn't an issue with prelude, it just happens that prelude-mode shadows the key bindings I use, which is possible in any other mode.

I followed this advice and created a minor mode for my key bindings, which allows me to turn them off if needed.

In a file in .emacs.d/personal I added the code in the linked post above and at the end of the file called (global-my-mode).

--
d

Tom Browder

unread,
May 25, 2016, 7:23:37 PM5/25/16
to Emacs Prelude
Derek, can you provide a github gist with the pertinent part of your personal init file (or here)?

Thanks,

-Tom

Derek Graham

unread,
Jun 2, 2016, 6:32:36 AM6/2/16
to Emacs Prelude
Hi Tom,

I think this is everything:

;;; Customised from http://emacs.stackexchange.com/a/358

(defvar my-keys-minor-mode-map (make-sparse-keymap)
  "Keymap while my-keys-minor-mode is active.")

(define-key my-keys-minor-mode-map (kbd "C-c g") 'goto-line)
;; ... etc

;; Prompt for confirmation beforeclosing
(define-key my-keys-minor-mode-map (kbd "C-x C-c") 'ask-before-closing)

;;;###autoload
(define-minor-mode my-keys-minor-mode
  "A minor mode so that my key settings override annoying major modes."
  :init-value nil
  :lighter " my-keys"
  :keymap my-keys-minor-mode-map)

;; Source: http://stackoverflow.com/questions/683425/globally-override-key-binding-in-emacs
(defadvice load (after give-my-keybindings-priority)
  "Try to ensure that my keybindings always have priority."
  (if (not (eq (car (car minor-mode-map-alist)) 'my-keys-minor-mode))
      (let ((mykeys (assq 'my-keys-minor-mode minor-mode-map-alist)))
        (assq-delete-all 'my-keys-minor-mode minor-mode-map-alist)
        (add-to-list 'minor-mode-map-alist mykeys))))
(ad-activate 'load)

;;;###autoload
(defun turn-on-my-keys-minor-mode ()
  "Turns on my-keys-minor-mode."
  (interactive)
  (my-keys-minor-mode 1))

;;;###autoload
(defun turn-off-my-keys-minor-mode ()
  "Turns off my-keys-minor-mode."
  (interactive)
  (my-keys-minor-mode -1))

;;;###autoload
(define-globalized-minor-mode global-my-keys-minor-mode my-keys-minor-mode turn-on-my-keys-minor-mode)

;; Turn off the minor mode in the minibuffer
(add-hook 'minibuffer-setup-hook #'turn-off-my-keys-minor-mode)

(provide 'my-keys-minor-mode)

;; Turn on for all buffers
(global-my-keys-minor-mode)

Reply all
Reply to author
Forward
0 new messages