Emacs: Xah Fly Keys Customization

By Xah Lee. Date: . Last updated: .

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

Set Command Mode Activation Key

Here's a example of how to set the command mode activation key.

;; make key 【f4】 key do activate command mode.
(global-set-key (kbd "<f4>") 'xah-fly-command-mode-activate)

For emacs key syntax see Emacs: How to Define Keys

On a typical PC keyboard, i recommend you make the Caps Lock key to send Home key signal for activating command mode.

(Home key by default will activate command mode.)

How to Make the CapsLock Key do Home Key

How to Make the CapsLock Key do Home Key

Set Insert Mode Activation Key

If you have a nice keyboard with extra thumb keys such as kinesis, ergodox, you can set a dedicated key to switch to insert mode.

For example:

;; make key 【end】 to activate insertion mode.
(global-set-key (kbd "<end>") 'xah-fly-insert-mode-activate)

Disable Changes to Control Key

xah-fly-keys supports standard control key such as 【Ctrl+o】 open, 【Ctrl+w】 close 【Ctrl+v】 paste 【Ctrl+z】 undo, etc.

You can disable them by:

(setq xah-fly-use-control-key nil) ; must come before loading xah-fly-keys

when disabled, no control binding will be touched by xah-fly-keys.

Disable Changes to Meta Key

The only meta keybinding xah-fly-keys bind is 【Alt+Space】 as a extra key for xah-fly-command-mode-activate (so, out of the box, it's convenient for people who haven't bothered setting Caps Lock.)

You can disable this by:

(setq xah-fly-use-meta-key nil) ; must come before loading xah-fly-keys

Make Escape Key Do C-g

You can make the Escape key do emacs's 【Ctrl+g】.

(define-key key-translation-map (kbd "ESC") (kbd "C-g"))

Note: this will work 99% of time. When it doesn't work, 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.)

Change Global Leader Key

You can set your own choice of the global leader key like this:

(define-key xah-fly-key-map (kbd "<f9>") xah-fly-leader-key-map) ; must come after loading xah-fly-keys

Put the above in your init, after loading xah-fly-keys.

Add or Change Leader Key Sequence

Here's how to change leader key sequence of a command.

Example, put this in your init, after loading xah-fly-keys:

;; make xah-fly-keys 【leader t】 do my-command-1
(define-key xah-fly-leader-key-map (kbd "t") 'my-command-1)
;; be sure that you want to replace existing command or keymap of 【leader t】

Here's a example of adding or creating a 3-key sequence:

;; make xah-fly-keys 【leader t x】 do sort-lines. (【leader t】 is qwerty 【leader k】)
(define-key xah-leader-t-keymap (kbd "x") 'sort-lines)

Here's a example of creating a whole keymap.

(setq my-nice-keymap (make-sparse-keymap))
(define-key my-nice-keymap (kbd "a") 'my-command-1)
(define-key my-nice-keymap (kbd "b") 'my-command-2)
(define-key my-nice-keymap (kbd "c") 'my-command-3)
(define-key my-nice-keymap (kbd "TAB") 'my-command-4)

;; make xah-fly-keys 【leader t】 as prefix for my-nice-keymap
(define-key xah-fly-leader-key-map (kbd "t") my-nice-keymap)
;; so now,
;; 【leader t a】 is my-command-1
;; 【leader t b】 is my-command-2
;; etc

Here's a helpful command to convert Dvorak key to QWERTY.

(xah--dvorak-to-qwerty "e") ; return "d"

Adding Hook

There are these hooks you can use:

Auto Save When Switching to Command Mode

By default, when you switch to command mode, current file is saved. It is done like this:

;; automatic save buffer when switching to command mode
(add-hook 'xah-fly-command-mode-activate-hook 'xah-fly-save-buffer-if-file)

You can remove hook like this:

;; remove a hook
(remove-hook 'xah-fly-command-mode-activate-hook 'xah-fly-save-buffer-if-file)

Change Cursor Shape, Command Mode, Insert Mode

You can make the mode state more obvious, by changing current line highlight. For example:

(defun hl-line-mode-on () (global-hl-line-mode 1))
(defun hl-line-mode-off () (global-hl-line-mode 0))

(add-hook 'xah-fly-command-mode-activate-hook 'hl-line-mode-on)
(add-hook 'xah-fly-insert-mode-activate-hook  'hl-line-mode-off)

You can also use background color, such as:

Advanced Customization, How Does xah-fly-keys Work

If you look at the file, in 10 minutes you should have a basic idea. The code is simple.

Here's a outline of the code, as of 2017-11-01. (Note: the implementation may change in the future. And i will update this page. But i always like to put a date stamp just in case.)

There's these 4 important command/variable:

xah-fly-command-mode-init looks like this:

(defun xah-fly-command-mode-init ()

     ("a" . execute-extended-command)
     ("b" . isearch-forward)
     ("c" . previous-line)
     ;; more here


The "a" "b" "c" there are Dvorak keys.

The function xah-fly--define-keys takes care of translating Dvorak to QWERTY, if user has qwery set (xah-fly-keys-set-layout "qwerty") .

xah-fly-insert-mode-init looks like this:

(defun xah-fly-insert-mode-init ()

     ("a" . nil)
     ("b" . nil)
     ("c" . nil)

     ;; more

xah-fly-leader-key-map looks like this:

 (define-prefix-command 'xah-fly-leader-key-map)

   ("a" . mark-whole-buffer)
   ("b" . end-of-buffer)
   ("c" . xah-fly-c-keymap)
   ("d" . beginning-of-buffer)
   ("e" . xah-fly-e-keymap)
   ("f" . xah-search-current-word)

   ;; more

For example, the line ("b" . end-of-buffer) means press 【leader b】 will call end-of-buffer.

Note: All keys in the file are Dvorak keys. For example, if you see ("b" . isearch-forward), that's Dvorak b, and when user has QWERTY set, that b is automatically translated to n.

back to Emacs: Xah Fly Keys

Like my tutorial? Put $5 at patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

Ask me question on patreon