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 Keys for Major Mode

Make a Key do x Depending on Major Mode

Russian Layout Addon

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

Remapping: Make Major Mode Use Xah Fly Keys Command Mode Key

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))

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)

2024-03-03 Note: the above is originally written by Will Dey in 2020-04 when he did a major rewrite of Xah Fly Keys key engine. Thanks to Will Dey.

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 you got a new keyboard.

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 current keyboard, and your workflow, but to someone using a different keyboard and different coding task, workflow, it may not be good.

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: