You can define your own minor mode and its key map and have that override all other modes (minor + major). That's exactly why I chose to write my own minor mode.
Steps to have your key bindings override all bindings:
- Defining your own minor mode and key map as shown below.
- Activate your minor mode globally
(define-key my-mode-map (kbd "C-j") #'newline-and-indent)
Similarly your other key bindings set in your minor mode will override those in other modes.
I highly recommend reading the blog post by Christopher Wellons on how to write a minor mode. That blog plus the annoyance of having to set multiple key bindings to nil
in multiple major and minor modes inspired me to write my own minor mode.
The best part of using this approach is that when you want to check what the key bindings do in emacs' default configuration, you simply turn off your minor mode; you then turn it back on and you get back your custom key bindings.
;; Main use is to have my key bindings have the highest priority
;; https://github.com/kaushalmodi/.emacs.d/blob/master/elisp/modi-mode.el
(defvar my-mode-map (make-sparse-keymap)
"Keymap for `my-mode'.")
;;;###autoload
(define-minor-mode my-mode
"A minor mode so that my key settings override annoying major modes."
;; If init-value is not set to t, this mode does not get enabled in
;; `fundamental-mode' buffers even after doing \"(global-my-mode 1)\".
;; More info: http://emacs.stackexchange.com/q/16693/115
:init-value t
:lighter " my-mode"
:keymap my-mode-map)
;;;###autoload
(define-globalized-minor-mode global-my-mode my-mode my-mode)
;; https://github.com/jwiegley/use-package/blob/master/bind-key.el
;; The keymaps in `emulation-mode-map-alists' take precedence over
;; `minor-mode-map-alist'
(add-to-list 'emulation-mode-map-alists `((my-mode . ,my-mode-map)))
;; Turn off the minor mode in the minibuffer
(defun turn-off-my-mode ()
"Turn off my-mode."
(my-mode -1))
(add-hook 'minibuffer-setup-hook #'turn-off-my-mode)
(provide 'my-mode)
;; Minor mode tutorial: http://nullprogram.com/blog/2013/02/06/