Emacs: Define Keybinding

By Xah Lee. Date: . Last updated: .

Define Key Shortcut Intro

In emacs, you can create any keyboard shortcut to any command.

For example, if you want Ctrl+t for whitespace-mode, put this in your Emacs Init File:

;; make Ctrl+t call whitespace-mode

;; for emacs 29, use this
(keymap-global-set "C-t" #'whitespace-mode)

;; before emacs 29, use this
(global-set-key (kbd "C-t") #'whitespace-mode)

If you are experimenting, and don't want to restart emacs every time you try to define a new key, you can place cursor at the end of parenthesis and Alt+x eval-last-sexp. The new key will be active right away. [see Evaluate Emacs Lisp Code]

If you made some mistake and need to start emacs without loading your init file, you can start emacs from terminal like this: emacs -q. [see Emacs: Init File Tutorial]

Keybinding Syntax

Emacs 29 New Keybinding Functions

Emacs 29 (Released 2023-07) made major changes in emacs lisp for doing keybinding.

The old functions still work, but new ones are better.

here are the new functions:

Remove a Keybinding

;; remove a keybinding

;; for emacs 29, use this
(keymap-global-set "C-t" nil)
;; or use
(keymap-global-unset "C-t")

;; before emacs 29, use this
(global-set-key (kbd "C-t") nil)
;; or
(global-unset-key (kbd "C-t"))

Find the Command of a Given Key

Alt+x describe-key, then type the key combination.

List Current Major Mode's Keys

Alt+x describe-mode. [see Emacs: What is Major Mode]

List ALL Keybinding

Alt+x describe-bindings.

Each Major Mode or Minor Mode usually add or change some keys. So, key list generated is specific to current buffer.

Swap CapsLock and Control Key

You cannot do it within emacs, because CapsLock is intercepted at the Operating System level, emacs does not see it. See:

Bad Key Choices

Best not to bind the following keys.

Ctrl+?

For historical reasons, Emacs treats the DEL character as the control equivalent of ?

Ctrl+h

C-h have a special status in emacs's key system for help-map.

For example, type Ctrl+x, then type Ctrl+h, it'll list what valid keys can follow and the associated command.

Help Functions (ELISP Manual)

Escape or Ctrl+[

The Escape key is tied to Ctrl+[ and Meta Key .

Escape by itself has complicated meanings depending when it is pressed and how many times it is pressed.

Ctrl+Shift+letter

In text terminals, it cannot distinguish shifted and unshifted versions of such combination. Works fine if you always use emacs in a GUI environment.

Other Char Bits (ELISP Manual)

Ctrl+m or Enter

These are the same by default. Normally they are bind to a command that sends ASCII 10 LINE FEED character.

Ctrl+i or Tab

These are the same by default. Normally they are bind to a command that sends ASCII 9 horizontal tab character.

Good Key Choices

Emacs has some 7 thousand commands. By default, 800 of them have key shortcuts. [see A Curious Look at Emacs One Thousand Keybindings] All the common key spots are used. If you define your own keys without care, you may find that many major mode or minor mode override your keys, because they have priority.

By official emacs documentation Key Binding Conventions (ELISP Manual) , the key space reserved for users are the function keys F5 to F9, and Ctrl+c letter. This is very restrictive.

The following keys are good spots for your own definitions, and does not cause any problems in practice.

F5, F6, F7, F8, F9, F11, F12

Not defined by emacs. Combination with Alt or Ctrl or Shift is also good. Make sure they are not used by the OS. Best is to create key sequence with them. [see Emacs: Define Key Sequence]

F1, F2, F3, F4, F10, F11

Their default commands are not frequently used, or, better with other keys. [see Emacs Key Layout Diagram]

Ctrl+0 to Ctrl+9, Alt+0 to Alt+9

By default they are digit-argument. Such is not very frequently used. If you need them, use the more general universal-argumentCtrl+u】 instead.

[see Emacs: Universal Argument (prefix arg)]

Number Pad Keys

Very useful, but depending on which emacs distro/OS you are using, or if you use emacs in terminal or GUI, binding these keys may not work. [see Emacs: Bind Number Pad Keys]

Practical Examples

System Wide Keybinding Setup

Get Programable Keyboard

Reference

Emacs, Change Keys