开发者

How to set a keybinding which is valid in all modes in Emacs [duplicate]

This question already has answers here: Globally override key binding in Emacs (8 answers) Closed 8 years ago.

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 i开发者_C百科n 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


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)


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]))))


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜