Emacs: Remapping Keys Using key-translation-map

By Xah Lee. Date: . Last updated: .

If you want to define a key for inserting a Unicode character such as BULLET , the best way is to use key-translation-map.

Example of using define-key with key-translation-map:

(define-key key-translation-map (kbd "<f8>") (kbd "•")) ; 【F8】 insert bullet char

More examples:

;; set keys to type Unicode
(define-key key-translation-map (kbd "<f9> u <down>") (kbd "↓"))
(define-key key-translation-map (kbd "<f9> u <left>") (kbd "←"))
(define-key key-translation-map (kbd "<f9> u <right>") (kbd "→"))
(define-key key-translation-map (kbd "<f9> u <up>") (kbd "↑"))

You can use it to swap keys.

;; swap keys
(define-key key-translation-map (kbd "<f11>") (kbd "<f12>"))
(define-key key-translation-map (kbd "<f12>") (kbd "<f11>"))

(info "(elisp) Translation Keymaps")

Problem Using global-set-key

You could also use global-set-key, but it has problems.

;; using global-set-key to insert unicode arrow. This won't work when in isearch
(global-set-key (kbd "<f8>") (lambda () (interactive) (insert "→")))

When you do interactive search 【Ctrl+s】, then when you type your key, it'll exit the search instead of inserting the char.


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 key combination. (Due to historical reasons, keyboard-translate does work for some Ctrl combination key. (thanks to Stefan Monnier and Deniz Dogan for this tip. (http://groups.google.com/group/gnu.emacs.help/msg/e0dc12074c776bda)))

Emacs Keybinding How-To Topic

  1. Emacs: How to Define Keys
  2. Emacs: Change Major Mode Keys
  3. Emacs: Change Minor Mode Keys
  4. Emacs: Fix Minor Mode Key Priority
  5. Emacs: Define Key Sequence
  6. Emacs: How to Bind Super Hyper Keys
  7. Emacs: How to Define Menu Key
  8. Emacs: Remapping Keys Using key-translation-map
  9. Emacs: Bind Number Pad Keys
  10. Emacs: How to Set Mouse Buttons
Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs