Emacs Keys: Change Major Mode Keys
General Steps to Change Major Mode Keys
To change or add keys to a Major Mode , add a function to the major mode's Hook.
- Find the major mode's name. 〔see Emacs: Find Major Mode Name〕
- Find the mode's hook name. 〔see Emacs: List All Hooks〕
- Add a function to the Hook .
- The function should contain code to change key, e.g.
(local-set-key (kbd "C-c C-c") #'gofmt)
Example: Add Key for Go-Mode
(defun my-golang-config () "For use in `go-mode-hook'." (local-set-key (kbd "C-c C-c") #'gofmt) ;; more here ) (when (fboundp 'go-mode) (add-hook 'go-mode-hook 'my-golang-config))
Example: Create Leader Key Sequence for Org Mode
;; make org mode's keys be key sequence of single keys, start with tab. (defvar xah-org-leader-map nil "A keymap for all `org-mode' keybinding.") (defun xah-set-org-mode-keys () "Config `org-mode'. Created: 2024-03-03 Version: 2024-04-30" (progn (define-prefix-command 'xah-org-leader-map) (define-key xah-org-leader-map (kbd "TAB") #'org-cycle) (define-key xah-org-leader-map (kbd "SPC RET") #'org-ctrl-c-ret) (define-key xah-org-leader-map (kbd "SPC TAB") #'org-ctrl-c-tab) (define-key xah-org-leader-map (kbd "a") #'org-attach) (define-key xah-org-leader-map (kbd "b") #'org-backward-heading-same-level) (define-key xah-org-leader-map (kbd "c") #'org-ctrl-c-ctrl-c) (define-key xah-org-leader-map (kbd "d") #'org-deadline) (define-key xah-org-leader-map (kbd "e") #'org-export-dispatch) (define-key xah-org-leader-map (kbd "f") #'org-forward-heading-same-level) (define-key xah-org-leader-map (kbd "g ,") #'org-date-from-calendar) (define-key xah-org-leader-map (kbd "g t") #'org-insert-structure-template) (define-key xah-org-leader-map (kbd "g .") #'org-goto-calendar) (define-key xah-org-leader-map (kbd "g 6") #'org-sort) (define-key xah-org-leader-map (kbd "g a") #'orgtbl-ascii-plot) (define-key xah-org-leader-map (kbd "g g") #'org-plot/gnuplot) (define-key xah-org-leader-map (kbd "g m b") #'org-previous-block) (define-key xah-org-leader-map (kbd "g m f") #'org-next-block) (define-key xah-org-leader-map (kbd "g m l") #'org-insert-last-stored-link) (define-key xah-org-leader-map (kbd "g m w") #'org-refile-copy) (define-key xah-org-leader-map (kbd "g s") #'org-ctrl-c-star) ;; more ) (define-key org-mode-map (kbd (or xah-major-leader-key "TAB")) xah-org-leader-map) ;; ) (add-hook 'org-mode-hook 'xah-set-org-mode-keys)
Modify Major Mode Keymap Directly
Alternatively, if you know the mode's keymap variable name, you can modify it directly.
(progn ;; modify dired keys (require 'dired) (when (boundp 'dired-mode-map) (define-key dired-mode-map (kbd "o") #'other-window) (define-key dired-mode-map (kbd "2") #'delete-window) (define-key dired-mode-map (kbd "3") #'delete-other-windows) (define-key dired-mode-map (kbd "4") #'split-window-below) (define-key dired-mode-map (kbd "C-o") #'find-file)))
Emacs, Change Keys
- Emacs Keys: Define Key
- Emacs Keys: Syntax
- Emacs Keys: Keybinding Functions (emacs 29)
- Emacs Keys: Good and Bad Key Choices
- Emacs Keys: Swap CapsLock Control
- Emacs Keys: Meta Key
- Emacs Keys: Change Major Mode Keys
- Emacs Keys: Change Minor Mode Keys
- Emacs Keys: Minor Modes Key Priority
- Emacs Keys: Change Minibuffer Keys