Add keybinding to js-mode/javascript-mode in Emacs

0

1

I have added web-beautify-* to Emacs 24, so that I can run M-x web-beautify-js to pretty format my javascript source code.

I have add some keybindings for html-mode and css-mode, which work fine.

(eval-after-load 'sgml-mode
  '(define-key html-mode-map (kbd "C-c b") 'web-beautify-html))
(eval-after-load 'css-mode
  '(define-key css-mode-map (kbd "C-c b") 'web-beautify-css))

However the following snippet for js-mode and javascript-mode don't work.

(eval-after-load 'javascript-mode
  '(define-key js-mode-map (kbd "C-c b") 'web-beautify-js))
(eval-after-load 'js-mode
  '(define-key js-mode-map (kbd "C-c b") 'web-beautify-js))

How can I add custom keybindings to js-mode and javascript-mode?

Will

Posted 2013-12-05T13:08:19.087

Reputation: 269

Answers

2

js-mode and its alias javascript-mode are defined in js.el, so you should use (eval-after-load 'js ...) instead. The reason it works for sgml-mode and css-mode is that those modes are defined in files with the same name as the mode itself.

legoscia

Posted 2013-12-05T13:08:19.087

Reputation: 2 197