Emacs: Remapping Keys Using key-translation-map

,

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 create a math symbols layout for inputting math 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 on any OS. 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 (⁖ on Mac & 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 particular to emacs.

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

If you want to define a key combo for inserting 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 built-in 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 Super & Hyper 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, 【Ctrl+w】 to select the char. Then when done, delete that character 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 【Ctrl+x odelete-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?

Using proper symbols can decreases ambiguity at syntax level. I use Unicode 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》 〔☛ Intro to Chinese Punctuation〕, and i use 【lenticular bracket】 to mark key combinations, 「corner bracket」 to mark computer code, and i use FULLWIDTH AMPERSAND (&) to avoid HTML entity complexity 〔☛ HTML Entities, Ampersand, Unicode, Semantics〕 , and lots others.

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

For coding, some languages heavily use math symbols (⁖ 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 (⁖ α = 3 instead of alpha = 3). See:

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

blog comments powered by Disqus