Emacs Keybinding Syntax Examples

By Xah Lee. Date: . Last updated: .

This page shows sample syntax for defining keys in emacs.

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

Single Modifier Key

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

Function keys and Special keys

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

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

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

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

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

(info "(elisp) Function Keys")

Menu/App Key

Emacs: How to Define Menu 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>") 'left-char) ; Alt+F3
(global-set-key (kbd "C-<f3>") 'left-char)  ; Ctrl+F3
(global-set-key (kbd "S-<f3>") 'left-char)  ; Shift+F3

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

2 Modifier Keys

2 modifier keys pressed simultaneously, plus a letter key:

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

Example of 2 modifier keys with a digit key:

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

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

3 Modifier Keys

3 modifier keys pressed simultaneously:

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

No Modifiers

A shortcut can be created without any modifier keys.

(global-set-key (kbd "3") 'left-char)
(global-set-key (kbd "a") 'left-char)
(global-set-key (kbd "é") 'left-char)
(global-set-key (kbd "α") 'left-char)
(global-set-key (kbd "你") 'left-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's Key Syntax Explained]

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
Liket it? Put $1 at patreon. Or Buy Xah Emacs Tutorial. Thanks.