I am trying to set up some keybindings for working with scheme. I have these two mode hooks which contain the bindings I want:

(add-hook 'scheme-mode-hook
          (lambda ()
            (local-set-key (kbd "C-j") 'scheme-send-last-sexp)))

(add-hook 'scheme-mode-hook
          (lambda ()
            (local-set-key (kbd "C-c C-j") 'scheme-send-region)))

The problem is that I also have these hooks defined which I use for elisp:

(eval-after-load "paredit"
  #'(define-key paredit-mode-map (kbd "C-j") 'eval-last-sexp))

(eval-after-load "paredit"
  #'(define-key paredit-mode-map (kbd "C-c C-j") 'eval-region))

My question is: how can I modify eval-after-load to set up my scheme key bindings only when in Scheme mode?

  • If you only use those paredit bindings for elisp, then they should be written like your scheme bindings but using the emacs-lisp-mode-hook or lisp-interaction-mode-hook. Commented May 11, 2015 at 17:47
  • @JordonBiondo I occasionally use them for Clojure too.
    – bitops
    Commented May 11, 2015 at 17:53
  • Write a function that locally creates bindings and add it to every major mode hook you want to use them in. That is the standard way to do it. Commented May 11, 2015 at 19:06
  • @JordonBiondo how is that different from the lambdas in the add-hook calls above?
    – bitops
    Commented May 11, 2015 at 20:39

2 Answers 2

  1. I think you mean set up only my scheme key bindings instead of "set up my scheme bindings only". Otherwise, if you do mean set up the scheme bindings only in Scheme mode then that should already be happening.

  2. If paredit-mode is a minor mode and scheme-mode is a major mode then you can't. A minor-mode keymap takes precedence over a major-mode keymap (aka local map).

    You can put one or the other command on a different key. But if you put both commands on the same key then the minor-mode binding wins.


I ended up solving this by unwinding paredit's keybindings first, then setting up my mode-hooks.

;; clear paredit bindings paredit mode
(eval-after-load "paredit"
  #'(define-key paredit-mode-map (kbd "C-j") nil))

(eval-after-load "paredit"
  #'(define-key paredit-mode-map (kbd "C-c C-j") nil))

(add-hook 'emacs-lisp-mode-hook
          '(lambda ()
             (local-set-key (kbd "C-c C-j") 'eval-region)
             (local-set-key (kbd "C-j") 'eval-last-sexp)))

(add-hook 'racket-mode-hook
          '(lambda ()
             (local-set-key (kbd "C-j") 'racket-send-last-sexp)
             (local-set-key (kbd "C-c C-j") 'racket-send-region)
             (local-set-key (kbd "C-c C-s") 'racket-repl)
             (local-set-key (kbd "C-c C-p") 'clipboard-yank)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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