Emacs Keybinding Syntax Examples

Buy Xah Emacs Tutorial. Master emacs benefits for life.
, , …,

This page shows sample syntax for defining keys in emacs.

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

Single Modifier Key

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

Function keys and Special keys

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

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

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

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

(global-set-key (kbd "<left>") 'backward-word)   ; (global-set-key (kbd "<right>") 'backward-word)  ; (global-set-key (kbd "<up>") 'backward-word)     ; (global-set-key (kbd "<down>") 'backward-word)   ; 
(global-set-key (kbd "RET") 'backward-word) ; Enter/Return key
(global-set-key (kbd "SPC") 'backward-word) ; Space bar key
;; On Linux, menu/apps key syntax is <menu>
(global-set-key (kbd "<menu> c") 'calendar) ;

;; Windows, menu/apps key syntax is <apps>
(global-set-key (kbd "<app> c") 'calendar) ;

See also: Emacs: How to Define Super & Hyper Keys.

(info "(elisp) Function Keys")

Key Sequence

Example of single key sequences.

(global-set-key (kbd "<f7>") 'nil) ; good idea to put nil to the starting key
(global-set-key (kbd "<f7> <f8>") 'calendar); F7 F8

Example of single key sequence starting with the ▤ Menu key.

;; Linux, the menu/apps key syntax is <menu>
;; Windows, the menu/apps key syntax is <apps>
(define-key key-translation-map (kbd "<apps>") (kbd "<menu>")) ; make the syntax equal

(global-set-key (kbd "<menu>") 'nil) ; good idea to put nil to the starting key
(global-set-key (kbd "<menu> b") 'nil) ; good idea to put nil to the starting key
(global-set-key (kbd "<menu> b w") 'calendar) ; Menu key followed by b then w

Example of sequence of single or chored 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-word) ; Alt+F3
(global-set-key (kbd "C-<f3>") 'backward-word)  ; Ctrl+F3
(global-set-key (kbd "S-<f3>") 'backward-word)  ; Shift+F3

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

2 Modifier Keys

2 modifier keys pressed simultaneously, plus a letter key:

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

Example of 2 modifier keys with a digit key:

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

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

3 Modifier Keys

3 modifier keys pressed simultaneously:

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

No Modifiers

A shortcut can be created without any modifier keys.

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

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
or share
blog comments powered by Disqus