Emacs Keybinding Syntax Examples

By Xah Lee. Date: . Last updated: .

This page shows sample syntax for defining keys in emacs.

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 keypad

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

(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 "RET") 'backward-char) ; Enter/Return key
(global-set-key (kbd "SPC") 'backward-char) ; Space bar key

(info "(elisp) Function Keys")

Menu/App Key

Emacs: How to Define Menu/App Key.

Super & Hyper Keys

Emacs: How to Define 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+“numberic 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. (see: Bucky bit) 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.

Emacs Keybinding Guide

For general guide on defining keys, see: Emacs: How to Define Keys.

Like it? Buy Xah Emacs Tutorial. Thanks.