Emacs: Xah Fly Keys

By Xah Lee. Date: . Last updated: .

This is the home page of xah-fly-keys.

xah-fly-keys.el is a modal editing mode for emacs, like vi, but the design of key/command choice is based on command frequency and ease-of-press key positions. Major editing key choices is mostly the same as in ergoemacs-mode.

xah-fly-keys is more efficient than vim, or any keybinding set in history.


Download at https://github.com/xahlee/xah-fly-keys

Also on MELPA http://melpa.org/#/xah-fly-keys 〔➤see Emacs: How to Install Packages Using ELPA, MELPA

Manual Install

Put the file xah-fly-keys.el in ~/.emacs.d/lisp/

put the following in your emacs init:

(add-to-list 'load-path "~/.emacs.d/lisp/")
(require 'xah-fly-keys)
(xah-fly-keys 1)

Key to Activate Command Mode

Call xah-fly-command-mode-activate to activate command mode.

By default, it has the following keys:

Alt+Space】 or ↖ Home or 【F8】 or 【▤ Menu

On a typical PC keyboard, i recommend you set left Alt or the Caps Lock key to send ↖ Home key signal for activating command mode.

How to Make the CapsLock Key Send Home Key Signal?

Key to Activate Insert Mode

While you are in command mode, 【u】 activates insertion mode. The command name is xah-fly-insert-mode-activate.

While you are in command mode, 【Space Space】 also activates insertion mode.

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

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

Lead Key for Commands

While in command mode, 【Space】 key acts as the leader key.

Globally, 【F9】 is also the leader key.

For example, query-replace is 【Space p】, while in command mode, or 【F9 p】 anywhere.

All emacs 【Ctrl+x …】 have a key that's a key sequence. (practically)

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

;; set a global leader key
(global-set-key (kbd "<f7>") 'xah-fly-leader-key-map)

I recommend to get a good symmetric keyboard with big thumb keys. See: Best Keyboard for Emacs

Emac's M-x

When in command mode:

Standard Control Keys

The following standard keys are supported by default.

(define-key xah-fly-key-map (kbd "<C-tab>") 'xah-next-user-buffer)
(define-key xah-fly-key-map (kbd "<C-S-iso-lefttab>") 'xah-previous-user-buffer)
(define-key xah-fly-key-map (kbd "C-v") 'yank)
(define-key xah-fly-key-map (kbd "C-w") 'xah-close-current-buffer)
(define-key xah-fly-key-map (kbd "C-z") 'undo)
(define-key xah-fly-key-map (kbd "C-n") 'xah-new-empty-buffer)
(define-key xah-fly-key-map (kbd "C-o") 'find-file)
(define-key xah-fly-key-map (kbd "C-s") 'save-buffer)
(define-key xah-fly-key-map (kbd "C-S-s") 'write-file)
(define-key xah-fly-key-map (kbd "C-S-t") 'xah-open-last-closed)
(define-key xah-fly-key-map (kbd "C-,") 'flyspell-goto-next-error)
(define-key xah-fly-key-map (kbd "C-+") 'text-scale-increase)
(define-key xah-fly-key-map (kbd "C--") 'text-scale-decrease)
(define-key xah-fly-key-map (kbd "C-0") (lambda () (interactive) (text-scale-set 0)))

you can disable them by:

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

dired mode single keys

In some modes, typing a letter key is a command. For example, in dired, 【m】 for mark, 【u】 for unmark.

When you are in insert mode, typing letter key will do whatever of that mode's commands.


Just open the file xah-fly-keys.el. Look at the code, it's really simple. If you prefer some other key or command, just change there.

Yes, i recommend you make a copy, and just modify the copy, and use that version. This make lots things simpler. Don't worry about upgrade. I still make experimental changes almost every week. If, after a year, some keys have changed, you can stick to your own version, or chose to upgrade. The current version i've been using for 3 years, plus 5 years of ergoemacs experience. xah-fly-keys's key system in current state is pretty much final.

The only difficulty is that you need to change muscle memory to adopt to this keyset. It'll take about 1 month to be fluent.

Kinesis Advantage, Ergodox, Configuration

See Kinesis Keyboard Optimal Thumb Cluster Layout

Truly Ergonomic Keyboard Configuration

xah-fly-keys is designed for any keyboard. Special care has taken to make sure it works well for standard PC keyboard, Apple Keyboards ⌨, Laptop Keyboards, and in text terminal.

If you have a fancy keyboard such as the following, much merrier:

I use it on Truly Ergonomic Keyboard. Here's my current Truly Ergonomic Keyboard configuration.

xah truly ergonomic keyboard 207 config 2013-10-01
xah Truly Ergonomic Keyboard config

note the ↖ Home and ↘ End keys. The ↖ Home is used to go to command mode.

the ↘ End is used as a leader key for xah's personal commands. (about 80 of them)

Also note the F13F17 keys. They are used as single key, mostly for controlling windows & tabs system-wide.

the “Apps” on the left space bar position means the ▤ Menu key. It is used as lead key for key sequences. Key sequence is used for non-fast-repeat commands, which is about all of emacs thousand commands, minus about 30 fast-repeat commands. 〔➤see Emacs: Fast-repeat vs Non-fast-repeat Commands & Keys

How does it compare to ergoemacs-mode?

ergoemacs-mode remaps the most frequencly used emacs commands to use 【Alt+key】, with key choice based on frequency of command call.

ergoemacs-mode also supports standard {Open 【Ctrl+o】, Close 【Ctrl+w】, New 【Ctrl+n】, …} keys familar to everyone. People who never use emacs can use it right away, without knowing any emacs keys.

ergoemacs-mode supports over 10 keyboard layouts, including: {QWERTY, Dvorak, Colemak, German, French, …}.

ergoemacs-mode is flexible. It provides many “themes”. You can pick your favorite keybinding set, or design your own.

ergoemacs-mode is also part of GNU Emacs, in ELPA repository.

If you don't like modal ways, I highly recommend ergoemacs-mode.

https://ergoemacs.github.io/ (ergoemacs-mode is lead by Matthew Fidler.)

How does it compare with evil-mode?

evil-mode lets you use vim keys in emacs. It has large user-base, mature, and with the advantage that you can also use vi/vim.


Why I Created It

here's some detail about why i created it.

i'm a QWERTY typer since ≈1987. Switched to Dvorak in ≈1994. Started to use emacs in 1997, live in it by 1998. Used default GNU keybinding up to 2006. Started to experience RSI discomfort a few times since 2010. 〔➤see Summary of My Typing/RSI Experience 1992 – 2013

i always regarded the vi modal ways a hack of the hack of the unix lineage. 〔➤see History of Emacs & vi Keys〕 I'll never touch it. But starting in 2010, i had lots thoughts about keyboard efficiency, and it occurred to me the modal way is actually more efficient, for programers.

the reason is, that on average, ≈50% of key-strokes of programers are actually not data-entry (that is, 50% are actually {moving cursor, copy, cut, delete, indent, switch buffer, open/close, completion, …}.) 〔➤see Emacs's Command Frequency Statistics〕 That means, if using modal ways, 50% of the time you don't have to press key combinations to execute commands, just a single key for each command. So, the 50% of time when you call commands, each command you save about 1 or 2 key-stroke, so in total you save about 50% × 50% = 25% of key-strokes. The trade-off is that you have to constantly switch command/insert modes, which means adding a key stroke every time you do so. In the end, i estimate that modal keyset saves you about 10% to 20% of key strokes.

i've been wanting to create and experiment with vi modal ways since 2010. While there's viper-mode (which is a vi-emulator) and there's evil-mode (which is based on vim (a more advanced vi)), but their key/command choices is largely historical, not a clean design based on efficiency.

see also: on vi Keybinding vs Emacs Keybinding

Like it? Buy Xah Emacs Tutorial.

or, buy something from my keyboard store.