Emacs: Remapping Keys Using key-translation-map

Perm url with updates: http://xahlee.org/emacs/emacs_key-translation-map.html

Emacs: Remapping Keys Using key-translation-map

Xah Lee, 2011-03-17

This page shows you how to remap keys to input unicode symbols using the elisp function “key-translation-map”, and discuss some related issues.

Recently i wrote a article on How to Create a APL or Math Symbols Keyboard Layout. In the article, i described several ways to remap keys on keyboard to input symbols for Mac, Windows, Linux. Some are OS-wide thru OS's user-configurable mechanisms. Some are using key-macro software for that OS. And there's emacs for just within emacs. I've used them all in the past 2 decades, but in the past few years, i find the most practical and flexible way is just emacs. Because: ① 99% of my typing are done in emacs. So, i don't really need them to be OS-wide. ② Each method of remapping keys all have their own limitations (e.g. on Mac and Windows, there's no way to remap some key combinations.), but within emacs it has the most support on remapping keys that has less restrictions than using OS's methods.

Here's interesting thing i discovered recently.

Using “global-set-key” or “key-translation-map” to Insert Unicode Char

If you want to define a key combo for typing a unicode char such as math symbol λ, you can do it using “global-set-key” or “key-translation-map”.

Example of using “global-set-key”:

(global-set-key (kbd "H-3") (lambda () (interactive) (insert "λ"))) ; 【Hyper+3】 insert λ char
(global-set-key (kbd "H-3") "λ") ; 【Hyper+3】 insert λ char. This is a builtin macro for above.

(In this example we use Hyper key, but Ctrl or Meta works too. You can make the Win key or the Menu key act as Hyper. To set up, see: Emacs: How to define Hyper & Super Keys.)

Example of using “key-translation-map”:

(define-key key-translation-map (kbd "H-3") (kbd "λ")) ; 【Hyper+3】 insert λ char

There are some advantage and disadvantages in either way.

Problem with “global-set-key”

If you use “global-set-key”, then when you do interactive search 【Ctrl+s】, then when you type 【Hyper+3】, it'll exit the search. So this means, if you use unicode heavily in your files, then you lose the ability to isearch them. (you can work-around by typing it in the buffer first, put cursor on it, start isearch, 【C-w】 to select the char. Then when done, delete that symbol you inserted for this purpose.)

Using “key-translation-map” doesn't have this problem.

Problem with “key-translation-map”

The problem with “key-translation-map” is this: Suppose you don't type the backtick (`) char often, and you press 【C-x o】 “delete-other-windows” ~50 times more often. So, to increase the efficiency of key use on keyboard, you remap backtick to do “delete-other-windows” and set 【Hyper+`】 for inputting the backtick. The elisp code looks like this:

(global-set-key (kbd "`") 'someCommand) ; set ` to someCommand
(define-key key-translation-map (kbd "H-`") (kbd "`")) ; insert backtick char by 【Hyper+`】

The problem is, when you type 【Hyper+`】, it'll actually call the command bound to backtick.

So, this means, the “key-translation-map” is great for symbol input but is not good if you want to use it as a way to remap buttons.

Note: there's the function “keyboard-translate”. However, it is designed to translate character only. So, key combination isn't a character and you can't use it for Hyper combination. Using (define-key key-translation-map …) is more versatile. (Due to historical reasons, “keyboard-translate” does work for some Ctrl combination key. (thanks to Stefan Monnier and Deniz Dogan for this tip. (Source groups.google.com)))

Who Need to Use Unicode That Often?

Well, i do, a lot, all over, in my writings, as well in coding.

For example, bullet (•), “curly quote”, dash (—), angle bracket for 〈article title〉 and 《book title》 (See: Intro to Chinese Punctuation with Computer Language Syntax Perspectives.), and i use 【lenticular bracket】 to mark key combinations, 「corner bracket」 to mark computer code, and i use FULLWIDTH AMPERSAND (&) for avoid html entity complexity (See: HTML Entities, Ampersand, Unicode, Semantics.) , and lots others.

Using proper symbols decreases ambiguity at syntax level. For example, the asterisk (*) can mean lots of things. But a dedicated bullet “•” carries a precise semantics.

For coding, some languages heavily use math symbols (e.g. APL, Mathematica). For functional languages such as Haskell, Scheme Lisp, you can setup for example “λ” to mean “lambda”, “≠” to mean “!=”, “⊕” for user-defined operators, etc. You can also use symbols for variable names in emacs lisp, Javascript, Java (e.g. “α”). See:

For more articles on keyboarding and unicode, see: Keyboards, Layouts, Hotkeys, Macros, RSI and Unicode Tutorial.

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs