Hi Alex, Thank you very much ! I will do what you said, and thank's for your explanation ! Boyang ------------------ Original ------------------ From: "Alex Kost"; Date: 2016年11月19日(星期六) 下午5:20 To: "boyang"; Cc: "help-gnu-emacs"; Subject: Re: Why can't local-set-key (kbd "n") in diff-mode ? boyang (2016-11-18 17:31 +0800) wrote: > Hi Hello! > Greetings, > I'm a Emacs newbie, when I do the following: > ---------------------------------------------------------------------------------------- > (add-hook 'diff-mode-hook > (lambda () > (local-set-key (kbd "n") 'next-error-no-select) > (local-set-key (kbd "p") 'previous-error-no-select))) > ---------------------------------------------------------------------------------------- I would do it like this: (with-eval-after-load 'diff-mode (define-key diff-mode-shared-map (kbd "n") 'next-error-no-select) (define-key diff-mode-shared-map (kbd "p") 'previous-error-no-select)) Such overriding of keys in the original maps always "works". The only thing you need is to find a map where the keys are bound. > It doesn't work, when I press "n" in "*Diff*", it stills run "diff-hunk-next" > when I "C-h k n", it still shows "diff-hunk-next". Same also for "p". > > But > 1. If I do the same thing in other mode, say "occur-mode-hook", it works in that mode. > 2. If I set other key other than "n" in "diff-mode-hook", say "x", it works. > > So is there somebody can tell me the reason behind this ? Good question! I've spent several minutes trying to understand this :-) So if you look at the code ("M-x find-function diff-mode"), there is this part: ;; Neat trick from Dave Love to add more bindings in read-only mode: (let ((ro-bind (cons 'buffer-read-only diff-mode-shared-map))) (add-to-list 'minor-mode-overriding-map-alist ro-bind) ...) This means that the keys from 'diff-mode-shared-map' will override the keys that you bind with 'local-set-key' (it modifies 'diff-mode-map'). Thus "n" and "p" are taken from 'diff-mode-shared-map'. But if you bind "x" key, it is taken from 'diff-mode-map' because 'diff-mode-shared-map' does not have "x" key (look at "M-x find-variable diff-mode-shared-map"). -- Alex