Emacs: Xah Fly Keys, Setup Keys for Major Mode

By Xah Lee. Date: . Last updated: .

Setup Major Mode Custom Keys

Normally, Major Mode keys start with Ctrl+Ctrl.

Here's the best way to create leader key set for any Major Mode , so you don't have to press Ctrl+c.

  1. Get a list of the major mode's commands you want. You do this by first activate the major mode (or open a file that invoke the major mode), then, Alt+x describe-mode, to list all commands of the mode.
  2. Then, decide on a leader key to call them. e.g. if leader key is Tab , you might have Tab a and Tab b and Tab c.
  3. Find out what's the keymap name in the major mode. (look at its source code), then do as follows.

Example: Setup Keys for Go language mode

Here's a example with go-mode.el.

;; example of adding a leader key map to golang mode
(when (fboundp 'go-mode)

  (defun xah-config-go-mode ()
    "config go-mode. Version 2021-01-15"
    (interactive)
    (progn
      ;; create a keymap
      (define-prefix-command 'xah-golang-leader-map)
      ;; add keys to it
      (define-key xah-golang-leader-map (kbd "c") 'xah-gofmt)
      (define-key xah-golang-leader-map (kbd "j") 'godef-jump)
      ;; add more of the major mode key/command here
      )
    ;; modify the major mode's key map, so that a key becomes your leader key
    (define-key go-mode-map (kbd "TAB") xah-golang-leader-map)
    ;;
    )

  (add-hook 'go-mode-hook 'xah-config-go-mode))

Setup Org-Mode Keys for Xah Fly Keys

emacs, Xah Fly Keys, customization