Emacs: Xah Fly Keys Customization

By Xah Lee. Date: . Last updated: .

This page shows how to customize Emacs: Xah Fly Keys.

Change Mode State Indicator

Disable Change to Emacs Control Keybinding

By default, Xah Fly Keys support standard ones like copy cut paste open, etc. When disabled, no control binding is changed by Xah Fly Keys.

;; put this BEFORE loading Xah Fly Keys
(setq xah-fly-use-control-key nil)

Disable Changes to Emacs Meta Keybinding

By default, Xah Fly Keys disables most of emacs meta keybinding, and add 2 or so.

You can disable this by:

;; put this BEFORE loading Xah Fly Keys
(setq xah-fly-use-meta-key nil)

Add a Global Key to Activate/Toggle Command/Insert Mode

Emacs Keybinding Syntax

This is helpful in finding the emacs syntax for keys:

Make the CapsLock Key do Home Key

Add Keys to Command Mode Keymap

;; put this AFTER loading Xah Fly Keys
(define-key xah-fly-command-map (kbd "x") 'command-name-x)

Add Keys in Insert Mode Keymap

;; put this AFTER loading Xah Fly Keys
(define-key xah-fly-insert-map (kbd "x") 'command-name-x)

Make Escape Key Do Cancel (C-g)

You can make the Escape key do emacs's Ctrl + g. (for cancel. Usually bound to keyboard-quit )

;; make esc key do cancel. works only in gui emacs
(define-key key-translation-map (kbd "<escape>") (kbd "C-g"))

Note: this works 99% of time. When it doesn't, just press Ctrl + g. (the only case i know it doesn't work is when you quit emacs, and emacs says there are unsaved file and if you still want to quit, and pressing Escape to cancel quit doesn't work, but Ctrl + g works.)

Note: for text terminal users, escape key is critical if you do not have Meta key setup. Because Meta + x can be typed by Escape x. So, if you remap Escape, you lose that.

Make Escape Key Do Both Activate Command Mode and Cancel

Add a Global Leader Key

You can add a global leader key, so you don't have to switch to command mode first. This is especially useful if you have a Foot Pedal or extra thumb keys.

;; put this AFTER loading Xah Fly Keys
(global-set-key (kbd "<f10>") xah-fly-leader-key-map)

Modify Leader Key Sequence

Xah Fly Keys Hooks

There are these hooks you can use:

xah-fly-command-mode-activate-hook
Hook variable. Value should be a list of function Symbols. When command mode is activated, these functions are called after activation.
xah-fly-insert-mode-activate-hook
Hook variable. Value should be a list of function Symbols. When insert mode is activated, these functions are called after activation.

Setup Major Mode Custom Keys

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. For example, if leader key is F9, you might have F9 a and F9 b and F9 c.
  3. Find out what's the keymap name in the major mode. (look at its source code), then do as follows. You can do this for org mode, magit 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 "<f9>") xah-golang-leader-map)
    ;;
    )

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

Make a Key do x Depending on Major Mode

Emacs: Mode-Dependent Key Shortcut

Russian Layout Addon

Emacs: Xah Fly Keys for Russian Layout

Start in Command Mode When Emacs Daemon Starts

;; 2021-03-10 this fix the problem of: when emacs start as daemon, xah fly keys is not in command mode. thx to David Wilson (daviwil)

(defun my/server-fix-up()
  "Make sure 'xah-fly-keys' is starting in command-mode.

https://github.com/xahlee/xah-fly-keys/issues/103
https://github.com/daviwil/emacs-from-scratch/blob/master/show-notes/Emacs-Tips-08.org#configuring-the-ui-for-new-frames"
  (xah-fly-keys-set-layout "dvorak")
  (xah-fly-keys t))

(if (daemonp)
    (add-hook 'server-after-make-frame-hook 'my/server-fix-up))

What Keyspots Are Available for Personal Keybinding?

You can bind keys to any of the spots for personal commands.

You can create a whole leader key map sequence for all your personal keys. For example, start with

xtodo WORK IN PROGRESS. the following are originally written by Will Dey in 2020-04 when he did a major rewrite of Xah Fly Keys key engine. It is in the process of editing and merged on this page.

Remapping

A very effective way to change what the command-mode keys do based on the major/minor mode is Remapping Commands. for example:

(define-key my-mode-map [remap command_name_1] 'command_name_2)

(info "(elisp) Remapping Commands")

Command remapping works regardless the keyboard layout of Xah Fly Keys, and allows you to make the customizations in the major/minor mode's map rather than the global xah-fly-command-map. Emacs automatically looks up remappings after finding the command bound to a key. Xah Fly Keys defines no command remappings, so they will always be looked up in the normal major and minor mode maps.

To add a remapping, find the command that Xah Fly Keys binds to a key in command mode (e.g. through describe-key), then add a binding in the major/minor mode's map like this:

(define-key major-or-minor-mode-map [remap xah-fly-command-name] 'mode-specific-command-name)

For example, to make the ā€œi j k lā€ keys scroll a PDF Tools page while in command mode:

(with-eval-after-load 'pdf-view
  (define-key pdf-view-mode-map [remap next-line] 'pdf-view-next-line-or-next-page)
  (define-key pdf-view-mode-map [remap previous-line] 'pdf-view-previous-line-or-previous-page))

Final Words

I recommend that you do not change much keys in Xah Fly Keys. Just stick to it. Because, one thing about creating a keybinding system is that there's a lot habit to overcome. I've been changing keys every month from about 2008 to 2017. Each time, it's extremely painful, and with strong desire to go back. And, once you start to explore keys, you often become biased towards your CURRENT workflow or work. So, you get into a situation that you change every few months, when project changes, or your choice of major mode changes, and in particular, when your keyboard changes. Also, if you don't have a year or two dedicated study about keybinding system, you create a key system that you think must be logically flawless, but actually just to your habit, your hands, your current keyboard, and your workflow, not to someone else.

xah-fly-keys's keybinding choices is not scientifically the best, because that is basically an illusive concept, but is near optimal.

If you are interested in keybinding efficiency research, be sure to read:

Xah Fly Keys