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+c.

You can make it so that all commands in the major mode uses a leader key sequence.

For example:

Example: Setup Keys for major mode

Here's the best way to do it:

  1. Get a list of the commands you want. You do this by first activate 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. i recommend Tab.
  3. Find the keymap name of the major mode. (look at its source code), then do as follows.
;; example of adding a leader key map to python mode

(defun my-config-python-mode ()
  "Config python-mode.
Version 2024-07-06"
  (interactive)

  (progn
    ;; create a keymap
    (define-prefix-command 'my-python-leader-map)

    ;; add keys to it
    (define-key my-python-leader-map (kbd "SPC" 'completion-at-point))
    (define-key my-python-leader-map (kbd "a" 'python-describe-at-point))
    (define-key my-python-leader-map (kbd "b" 'python-shell-send-statement))
    (define-key my-python-leader-map (kbd "c" 'python-eldoc-at-point))
    (define-key my-python-leader-map (kbd "d" 'imenu))
    (define-key my-python-leader-map (kbd "e" 'python-shell-send-file))
    (define-key my-python-leader-map (kbd "f" 'run-python))
    (define-key my-python-leader-map (kbd "g" 'python-shell-send-region))
    (define-key my-python-leader-map (kbd "h" 'python-shell-send-string))
    (define-key my-python-leader-map (kbd "i" 'python-check))
    (define-key my-python-leader-map (kbd "j" 'python-shell-switch-to-shell))
    (define-key my-python-leader-map (kbd "," 'python-indent-shift-left))
    (define-key my-python-leader-map (kbd "." 'python-indent-shift-right))

    ;; more here
    )

  ;; modify the major mode key map, so that a key becomes your leader key
  (define-key python-mode-map (kbd "TAB") my-python-leader-map)
  ;;
  )

(when (fboundp 'python-mode)
  (add-hook 'python-mode-hook 'my-config-python-mode))

Setup Org-Mode Keys for Xah Fly Keys

emacs, Xah Fly Keys, customization