Emacs Keybinding Syntax

By Xah Lee. Date: . Last updated: .

Find Key Syntax

Alt+x describe-key, then press the key you want. Emacs will then display its syntax.

For example, suppose you want to know the syntax for the key press of Ctrl+Alt+F8. First, do Alt+x describe-key, then press Ctrl+Alt+F8, then emacs will print “<C-M-f8> is undefined”. That means, you can use (kbd "<C-M-f8>") to represent that key combination in lisp code. (Note: emacs has lot syntax variations for a given keyboard shortcut, but the one printed by describe-key is guaranteed to work. For details of emacs's keystroke syntax variation, see: Emacs Key Syntax Explained.)

Examples

In the following, the backward-char is a example command. Replace it with the command name you want.

Single Modifier Key

(global-set-key (kbd "M-a") 'backward-char) ; Alt+a
(global-set-key (kbd "C-a") 'backward-char) ; Ctrl+a

Function keys and Special keys

(global-set-key (kbd "<f3>")   'backward-char)   ; F3 key
(global-set-key (kbd "<kp-3>") 'backward-char)   ; the “3” key on number pad

(global-set-key (kbd "<insert>") 'backward-char) ; Ins key
(global-set-key (kbd "<delete>") 'backward-char) ; Del key ⌦ (forward delete)

(global-set-key (kbd "<home>") 'backward-char)
(global-set-key (kbd "<end>") 'backward-char)

(global-set-key (kbd "<next>") 'backward-char)   ; page down key
(global-set-key (kbd "<prior>") 'backward-char)  ; page up key

(global-set-key (kbd "<left>") 'backward-char)   ; (global-set-key (kbd "<right>") 'backward-char)  ; (global-set-key (kbd "<up>") 'backward-char)     ; (global-set-key (kbd "<down>") 'backward-char)   ; 
(global-set-key (kbd "DEL") 'backward-char) ; backspace key ⌫
(global-set-key (kbd "RET") 'backward-char) ; Enter/Return key
(global-set-key (kbd "SPC") 'backward-char) ; Space bar key

(info "(elisp) Function Keys")

Menu/App Key

Emacs: Bind Menu/App Key.

Super and Hyper Keys

Emacs: How to Bind Super Hyper Keys.

Key Sequence

;; Example of single key sequence
(global-set-key (kbd "<f7>") nil) ; good idea to put nil to the starting key
(global-set-key (kbd "<f7> <f8>") 'calendar)
(global-set-key (kbd "<f7> <f9>") 'calc)
;; Example of sequence of single or chord keys

(global-set-key (kbd "C-e") nil) ; good idea to put nil to the starting key
(global-set-key (kbd "C-e a") 'calendar)  ; Ctrl+e a
(global-set-key (kbd "C-e SPC") 'calendar)  ; Ctrl+e Space
(global-set-key (kbd "C-e C-a") 'calendar); Ctrl+e Ctrl+a

1 Modifier + Special Key

(global-set-key (kbd "M-<f3>") 'backward-char) ; Alt+F3
(global-set-key (kbd "C-<f3>") 'backward-char)  ; Ctrl+F3
(global-set-key (kbd "S-<f3>") 'backward-char)  ; Shift+F3

(global-set-key (kbd "M-<up>") 'backward-char)  ; Alt+↑
(global-set-key (kbd "C-<up>") 'backward-char)  ; Ctrl+↑
(global-set-key (kbd "S-<up>") 'backward-char)  ; Shift+↑

2 Modifier Keys

2 modifier keys pressed simultaneously, plus a letter key:

(global-set-key (kbd "M-A") 'backward-char) ; Alt+Shift+a
(global-set-key (kbd "C-A") 'backward-char) ; Ctrl+Shift+a
(global-set-key (kbd "C-M-a") 'backward-char) ; Ctrl+Alt+a

Example of 2 modifier keys with a digit key:

(global-set-key (kbd "M-#") 'backward-char)       ; Alt+Shift+3 or Alt+#
(global-set-key (kbd "C-#") 'backward-char)       ; Ctrl+Shift+3 or Ctrl+#
(global-set-key (kbd "C-M-3") 'backward-char)     ; Ctrl+Alt+3

(global-set-key (kbd "C-S-<kp-3>") 'backward-char); Ctrl+Shift+“number pad 3”

3 Modifier Keys

3 modifier keys pressed simultaneously:

(global-set-key (kbd "C-M-S-a") 'backward-char)   ; Ctrl+Alt+Shift+a
(global-set-key (kbd "C-M-!") 'backward-char)     ; Ctrl+Alt+Shift+1 or Ctrl+Alt+!
(global-set-key (kbd "C-M-S-<up>") 'backward-char); Ctrl+Alt+Shift+↑

No Modifiers

A shortcut can be created without any modifier keys.

(global-set-key (kbd "3") 'backward-char)
(global-set-key (kbd "a") 'backward-char)
(global-set-key (kbd "é") 'backward-char)
(global-set-key (kbd "α") 'backward-char)
(global-set-key (kbd "你") 'backward-char)

Emacs's keybinding system is based on character input, not on key-presses. That's why you can define a key such that when the character alpha α is entered, it invokes the command. For how to enter Unicode characters, see Emacs: Unicode Tutorial.

[see Emacs Key Syntax Explained]

ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Emacs Keys

Overview

How-To

Key Tips

Emacs Pinky

Keybinding Design

Misc