Emacs: Change Major Mode Keys

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

Emacs Keybinding How-To Topic

  1. Emacs: How to Define Keys
  2. Emacs: Change Major Mode Keys
  3. Emacs: Change Minor Mode Keys
  4. Emacs: Fix Minor Mode Key Priority
  5. Emacs: Define Key Sequence
  6. Emacs: How to Bind Super Hyper Keys
  7. Emacs: How to Define Menu Key
  8. Emacs: Remapping Keys Using key-translation-map
  9. Emacs: Bind Number Pad Keys
  10. Emacs: How to Set Mouse Buttons
Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs