How to Define Keyboard Shortcuts in Emacs

Xah Lee, 2005, 2006, 2007-12.

In emacs, you can create any keyboard shortcut to any commands to your liking. This page shows you how.

The act of creating a keyboard shortcut is called keybinding. For example, if you want “Ctrl+z” to be undo, then, place this code “(global-set-key (kbd "C-z") 'undo)” in your emacs customization file (~/.emacs) and restart emacs (or, “Alt+x eval-buffer”).

Here are some sample code you need to place in your emacs init file for defining various key press combinations. The “func-name” is the name of the function you want the keypress to invoke.

Examples

Single Modifier Key

Example of shortcuts with a single modifier key:

(global-set-key (kbd "M-a") 'func-name) ; Meta+a
(global-set-key (kbd "C-a") 'func-name) ; Ctrl+a

Function keys and Special keys

Example of shortcuts with a special key:

(global-set-key (kbd "<f2>")   'func-name)   ; F2 key
(global-set-key (kbd "<kp-2>") 'func-name)   ; the “2” key on the number keypad

(global-set-key (kbd "<insert>") 'func-name) ; the Ins key
(global-set-key (kbd "<delete>") 'func-name) ; the Del key

(global-set-key (kbd "<home>") 'func-name)
(global-set-key (kbd "<end>") 'func-name)

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

(global-set-key (kbd "<left>") 'func-name)   ; (global-set-key (kbd "<right>") 'func-name)  ; (global-set-key (kbd "<up>") 'func-name)     ; (global-set-key (kbd "<down>") 'func-name)   ; 
(global-set-key (kbd "RET") 'func-name) ; the Enter/Return key
(global-set-key (kbd "SPC") 'func-name) ; the Space bar key

Modifier + Special Key

Example of shortcuts with a modifier key and a special key:

(global-set-key (kbd "M-<f2>") 'tfunc-name) ; Meta+F2
(global-set-key (kbd "C-<f2>") 'func-name)  ; Ctrl+F2
(global-set-key (kbd "S-<f2>") 'func-name)  ; Shift+F2

(global-set-key (kbd "M-<up>") 'func-name)  ; Meta+↑
(global-set-key (kbd "C-<up>") 'func-name)  ; Ctrl+↑
(global-set-key (kbd "S-<up>") 'func-name)  ; Shift+↑

Two Modifier Keys

Example of shortcuts with 2 modifier keys pressed simultaneously, plus a letter key:

(global-set-key (kbd "M-A") 'func-name) ; Meta+Shift+a
(global-set-key (kbd "C-A") 'func-name) ; Ctrl+Shift+a
(global-set-key (kbd "C-M-a") 'func-name) ; Ctrl+Meta+a

Example of 2 modifier keys with a digit key:

(global-set-key (kbd "M-@") 'func-name)       ; Meta+Shift+2 or Meta+@
(global-set-key (kbd "C-@") 'func-name)       ; Ctrl+Shift+2 or Ctrl+@
(global-set-key (kbd "C-M-2") 'func-name)     ; Ctrl+Meta+2

(global-set-key (kbd "C-S-<kp-2>") 'func-name); Ctrl+Shift+“numberic pad 2”

Note: A keypress-combination such as “Meta+Shift+2” can also be considered as “Meta+@”. So, in emacs, you might be thinking that both of these code: “(kbd "M-S-2")” and “(kbd "M-@")” will work. Actually, only the latter will work.

When writing a keybinding definition, for a key combination that involves the Shift key, you must use a version without the shift key if possible.

GOODBAD
(kbd "M-A")(kbd "M-S-a")
(kbd "M-@")(kbd "M-S-2")
(kbd "M-:")(kbd "M-S-;")

Example of shortcut with a punctuation key that are typed with Shift:

(global-set-key (kbd "C-:") 'func-name) ; Ctrl+Shift+; or Ctrl+:
(global-set-key (kbd "C-\"") 'func-name) ; Ctrl+Shift+' or Ctrl+"
; note: the question mark “?” cannot be used in shortcut.

Example of 2 modifier keys with a special key:

(global-set-key (kbd "M-S-<f1>") 'func-name)   ; Meta+Shift+F1
(global-set-key (kbd "C-S-<kp-2>") 'func-name) ; Ctrl+Shift+“numberic pad 2”
(global-set-key (kbd "C-M-<up>") 'func-name)   ; Ctrl+Meta+↑

When there are more than one modifier keys, such as “C-M-a”, the order of the modifier in the string does not matter. It is recommended that they be alphabetical. So, use “C-M-a”, not “M-C-a”.

Three Modifier Keys

Example of 3 modifier keys:

(global-set-key (kbd "C-M-S-a") 'func-name)   ; Ctrl+Meta+Shift+a
(global-set-key (kbd "C-M-!") 'func-name)     ; Ctrl+Meta+Shift+1 or Ctrl+Meta+!
(global-set-key (kbd "C-M-\"") 'func-name)    ; Ctrl+Meta+Shift+' or Ctrl+Meta+"
(global-set-key (kbd "C-M-S-<up>") 'func-name); Ctrl+Meta+Shift+↑

Key Sequence

Example of shortcuts with a sequence of keystrokes:

(global-set-key (kbd "C-c a") 'func-name)  ; Ctrl+c a
(global-set-key (kbd "C-c SPC") 'func-name)  ; Ctrl+c Space
(global-set-key (kbd "C-c <f2>") 'func-name)   ; Ctrl+c f2
(global-set-key (kbd "C-c <up>") 'func-name)   ; Ctrl+c ↑

(global-set-key (kbd "C-c C-c <up>") 'func-name); Ctrl+c Ctrl+c ↑

No Modifiers

A shortcut can be created without any modifier keys.

(global-set-key (kbd "2") 'func-name)
(global-set-key (kbd "a") 'func-name)
(global-set-key (kbd "é") 'func-name)
(global-set-key (kbd "α") 'func-name)
(global-set-key (kbd "π") 'func-name)
(global-set-key (kbd "(") 'func-name)
(global-set-key (kbd "你") 'func-name)

Hyper and Super keys

Emacs support keyboards that has Super and Hyper modifier keys. In today's Windows keyboard or Mac keyboard, there's no such modifier keys, but you can make the Windows keyboard's Windows key↗, Menu key↗, or Mac keyboard's Option key↗ to function as Super or Hyper within emacs. Here's the lisp code to do it:

; setting the PC keyboard's various keys to Super or Hyper
(setq w32-pass-lwindow-to-system nil 
      w32-pass-rwindow-to-system nil 
      w32-pass-apps-to-system nil 
      w32-lwindow-modifier 'super ;; Left Windows key 
      w32-rwindow-modifier 'super ;; Right Windows key 
      w32-apps-modifier 'hyper) ;; Menu key

; various settings for the Mac keyboard
(setq mac-option-modifier 'hyper) ; sets the Option key as Hyper
(setq mac-option-modifier 'super) ; sets the Option key as Super
(setq mac-command-modifier 'meta) ; sets the Command key as Meta
(setq mac-control-modifier 'meta) ; sets the Control key as Meta

The syntax for defining keys with the Super or Hyper modifier keys is the same as Meta and Control. Use “H” for Hyper, “s” for Super. Example:

(global-set-key (kbd "H-b") 'func-name) ; H is for hyper
(global-set-key (kbd "s-b") 'func-name) ; lower case “s” is for super

(global-set-key (kbd "M-H-b") 'func-name) ; Meta+Hyper+b
(global-set-key (kbd "M-s-b") 'func-name) ; Meta+Super+b

Common Questions

How to unset a keybinding?

To unset a keybinding, use “global-unset-key”. For example, you have defined a keystroke for undo, and wants to kick the habit of the hitting the default shortcut for undo:

(global-unset-key (kbd "C-_"))

How to find out what key is bind to what command?

Type “Ctrl+h k” (or “M+x describe-key”), then type the key combination. Emacs will then show the function that key press is bound to.

To see a list of ALL current keybindings, use describe-bindings (Ctrl+h b).

How to find out the syntax for a particular key press?

Type “Ctrl+h k” (or “M+x describe-key”), then press the key combination. Emacs will then tell you its read syntax as a keyboard macro. For example, you want to know the syntax for the key press of “Ctrl+Alt+F8”. Type “M+x describe-key” then press “Ctrl+Alt+F8”, then emacs will print “<C-M-f8> is undefined”. Then, you can use “(kbd "<C-M-f8>")” to represent that key press combination in lisp code.

Note: There is a lot syntax variations, but the one printed by “describe-key” is guaranteed to work. For details of emacs's keystroke syntax variation, see: The Confusion of Emacs's Keystroke Representation.

How to have a specific keyboard shortcut set only when a particular mode is active?

Use mode hook, which loads your code only when the mode is loaded. Then, use define-key on a specific keymap. Here's a usable example:

(add-hook 'html-mode-hook
 (lambda ()
 (define-key html-mode-map (kbd "C-c w") 'bold-word)
 (define-key html-mode-map (kbd "C-c b") 'blue-word)
 (define-key html-mode-map (kbd "C-c p") 'insert-p)
 (define-key html-mode-map (kbd "M-4") 'tag-image)
 (define-key html-mode-map (kbd "M-5") 'wrap-url)
 )
)

Reference: (info "(emacs)Hooks").

Practical Uses

Here are some practical real-world ideas on how defining keyboard shortcut is used.

Insert Special Characters

You can create a shortcut to insert your favorite special character. You can add few chars, or a systematic character set.

(global-set-key (kbd "M-i a") "α")
(global-set-key (kbd "M-i b") "β")
(global-set-key (kbd "M-i t") "θ")
(global-set-key (kbd "<kp-6>") "→")

See also: Emacs and Unicode Tips, Mac OS X Keybinding and Unicode Tips.

Remap Frequently Used Shortcuts

;; make cursor movement keys under right hand's home-row.
(global-set-key (kbd "M-j") 'backward-char) ; was indent-new-comment-line
(global-set-key (kbd "M-l") 'forward-char)  ; was downcase-word
(global-set-key (kbd "M-i") 'previous-line) ; was tab-to-tab-stop
(global-set-key (kbd "M-k") 'next-line) ; was kill-sentence

(global-set-key (kbd "M-SPC") 'set-mark-command) ; was just-one-space
;; type parens in pairs with Hyper and right hands's home-row
(global-set-key (kbd "H-j") (lambda () (interactive) (insert "{}") (backward-char 1)))
(global-set-key (kbd "H-k") (lambda () (interactive) (insert "()") (backward-char 1)))
(global-set-key (kbd "H-l") (lambda () (interactive) (insert "[]") (backward-char 1)))

For a more systematic change, see A Ergonomic Keyboard Shortcut Layout For Emacs.

Use It For Template Insertion

(add-hook 'html-mode-hook
 (lambda ()
 (define-key html-mode-map (kbd "<F5>") 'insert-my-header)
 (define-key html-mode-map (kbd "<F6>") 'insert-my-footer)
 (define-key html-mode-map (kbd "<F7>") 'insert-signature)
 (define-key html-mode-map (kbd "<F8>") 'insert-paragraph-tag)
 ; ...
 )
)

(defun 'insert-paragraph-tag ()
  "Insert <p></p> at cursor point."
  (interactive)
  (insert "<p></p>")
  (backward-char 4))

Use It To Open Frequently Used Files

(global-set-key (kbd "<F5>") 'open-unicode-template)
(global-set-key (kbd "<F6>") 'open-my-file1)
(global-set-key (kbd "<F7>") 'open-my-file2)
; ...

(defun open-unicode-template ()
 "Open a file containing frequently used unicode chars"
 (interactive)
 (find-file "~/web/emacs/unicode.txt"))

References And Further Readings:


Related essays:


Page created: 2005-08.
© 2005 by Xah Lee.
Xah Signet