In my .emacs file, I want to add a key binding for a specific major mode (setting coffee-compile-file to C-c C-c for coffee-mode). I've found a lot of instructions on using local-set-key and global-set-key, so I can easily add this binding once I've opened a file in coffee-mode, but it would be nice for this to be handled by .emacs.

link|improve this question
feedback

2 Answers

up vote 3 down vote accepted

Use the mode hook. C-h m shows information about the major mode, usually including what hook(s) it supports; then you do something like

(add-hook 'coffee-mode-hook ;; guessing
    '(lambda ()
       (local-set-key "\C-cc" 'coffee-compile-file)))
link|improve this answer
feedback

You can define the key in the mode specific map, something like:

(add-hook 'coffee-mode-hook
    (lambda ()
        (define-key coffee-mode-map (kbd "C-c c" 'coffee-compile-file)))
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.