Emacs: How to Bind Keys for Major Mode?

By Xah Lee. Date: . Last updated: .

How to change/add keys to a major mode?

Use a hook for the mode. A hook will load your code whenever that mode is activated.

;; example of binding keys only when html-mode is active

(defun my-html-mode-config ()
  "For use in `html-mode-hook'."
  (local-set-key (kbd "<C-right>") 'sgml-skip-tag-forward) ; add a key
  (local-set-key (kbd "<C-left>") 'sgml-skip-tag-backward) ; add a key
  (local-set-key (kbd "C-c C-p") nil) ; example of remove a key
  ;; more here

;; add to hook
(add-hook 'html-mode-hook 'my-html-mode-config)

What's Hook?

Emacs: What's Hook?

Avoid Lambda in Hook

Emacs: Avoid Lambda in Hook

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs