0

I've configured my emacs to use M-j as backward-char by

(global-set-key (kbd "M-j") 'backward-char) ; was indent-new-comment-line

in my .emacs file. This works fine in many modes (text/org/lisp).

But in c++-mode & php-mode it is bound to the default c-indent-new-comment-line
How can I bind M-j to use backward-char in these modes too.
And in general for ALL modes.

Thanks,
AnotherEmacsLearner

4 Answers4

10

There are policies about which keys are supposed to be mode-dependent and which not. You can overrule bindings changed by a specific mode, but it is a hassle and has to be done for every mode you will be using. It is smarter to keep your own cross-cutting neato bindings to keys that major modes will not touch out of principle. I particularly like the F1-F12 keys for that, or the Sun Function keys when I can get them. The C-c + letter sequences are also explicitly reserved for user-defined commands and will not be rebound by major modes. (See: Key Binding Conventions)

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
Kilian Foth
  • 13,904
  • 5
  • 39
  • 57
4

I unset keys that are in the way for specific modes like this:

(add-hook 'gnus-summary-mode-hook
          (function (lambda ()
                      (local-unset-key '[M-down])
                      (local-unset-key '[M-up]))))

(add-hook 'org-mode-hook
          (function (lambda ()
                      (local-unset-key '[S-down])
                      (local-unset-key '[S-left])
                      (local-unset-key '[S-right])
                      (local-unset-key '[S-up]))))
aerique
  • 982
  • 5
  • 21
1

This issue was addressed in this question. The way to do this is to create a minor mode with your bindings. Your minor mode bindings will shadow any major mode's bindings.

I like your example, since just within the last couple of weeks, I rebound M-h,j,k,l to their equivalent vim movements, and made a minor mode to do it (It turned out to be a great idea. Emacs's default bindings really are terrible). Here's a sample of some of my code:

(defvar kirkland-minor-mode-map (make-keymap) "kirkland-minor-mode keymap.")
(define-key kirkland-minor-mode-map (kbd "M-h") 'backward-char)
(define-key kirkland-minor-mode-map (kbd "M-l") 'forward-char)
(define-key kirkland-minor-mode-map (kbd "M-j") 'next-line)
(define-key kirkland-minor-mode-map (kbd "M-k") 'previous-line)
(define-minor-mode kirkland-minor-mode
  "A minor mode so that my key settings aren't shadowed by other major/minor modes"
  t " kirkland" 'kirkland-minor-mode-map)

One other thing I should mention is that while this will override any major mode bindings, it can still be overridden by other minor modes which are loaded later.

Community
  • 1
  • 1
Kirkland
  • 164
  • 1
  • 5
0

Nothing can stop any mode from redefining any key any way it wants and it always shadows the global-set-key. So you have to redefine it for every mode that redefines it:

(defun redefine-cc-mode-keys ()
  (define-key c-mode-base-map "M-J" 'backward-char))
(add-hook 'c-initialization-hook 'redefine-cc-mode-keys)

or similar.

Laurynas Biveinis
  • 10,547
  • 4
  • 53
  • 66