Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define key-chord key with specific mode

Tags:

emacs

How do you define a key-chord key only in specific mode, for example I want to bind a cider repl to a specific key only in clojure-mode or cider-mode. I can only find an example that activates the key globally.

Thanks for your help.

EDIT:

(require 'evil)
(require 'key-chord)
(evil-mode 1)

(key-chord-mode 1)
(key-chord-define evil-insert-state-map "jk" 'evil-normal-state)
(key-chord-define-global "gt" 'other-window)
(key-chord-define clojure-mode-hook "gj" 'cider-jack-in)
;; error : Wrong type argument: keymapp, (rainbow-delimiters-mode)


(provide 'init-evil)
like image 468
Faris Nasution Avatar asked Apr 24 '14 07:04

Faris Nasution


1 Answers

Defining mode-specific key bindings

Here is an example of how to do this:

(define-key clojure-mode-map (kbd "C-c r") 'cider-repl)

... where of course you would have to replace cider-repl with the specific command you want to bind. Note that the quote ' before the command name is required.

To generalize:

(define-key <mode-map> <key-binding> '<command>)

key-chord-specific instructions

You need to change the line where you're trying to set up the clojure-mode-specific key binding to

(add-hook 'clojure-mode-hook 
          (lambda () (key-chord-define clojure-mode-map "gj" 'cider-jack-in)))

Appendix: Making sure mode-maps are defined before modifying them

In order for modifications to clojure-mode-map to work properly, you have to make sure it is defined when you call define-key as described above.

If you are using the Emacs Package Manager, you are likely to have this line

(package-initialize)

somewhere in your .emacs file (which takes care of loading packages installed via package-install). Make sure you call define-key somewhere below this line.

Alternatively you can add the call to define-key to the hook that is run when clojure-mode is enabled:

(defun clojure-set-up-key-bindings ()
  (define-key clojure-mode-map (kbd "C-c r") 'cider-repl)
  ;; If necessary, add more calls to `define-key' here ...
  )

(add-hook 'clojure-mode-hook 'clojure-set-up-key-bindings)
like image 106
itsjeyd Avatar answered Oct 19 '22 11:10

itsjeyd