Emacs Keys: Define Key

By Xah Lee. Date: . Last updated: .

How to Assign Key to Command

In emacs, you can create any keyboard shortcut to any command.

For example, if you want Ctrl+t for whitespace-mode, put this in your Emacs Init File:

;; make Ctrl+t call whitespace-mode

;; for emacs 29, use this
(keymap-global-set "C-t" #'whitespace-mode)

;; before emacs 29, use this
(global-set-key (kbd "C-t") #'whitespace-mode)

If you are experimenting, and don't want to restart emacs every time you try to define a new key, you can place cursor at the end of parenthesis and Alt+x eval-last-sexp. The new key will be active right away. [see Evaluate Emacs Lisp Code]

If you made some mistake and need to start emacs without loading your init file, you can start emacs from terminal like this: emacs -q. [see Emacs: Init File Tutorial]

Remove a Keybinding

;; remove a keybinding

;; for emacs 29, use this
(keymap-global-set "C-t" nil)
;; or use
(keymap-global-unset "C-t")

;; before emacs 29, use this
(global-set-key (kbd "C-t") nil)
;; or
(global-unset-key (kbd "C-t"))

Find the Command Name of a Given Key

Alt+x describe-key, then type the key combination.

List Current Major Mode's Keys

Alt+x describe-mode. [see Emacs: Major Mode]

List ALL Keybinding

Alt+x describe-bindings.

Each Major Mode or Minor Mode usually add or change some keys. So, key list generated is specific to current buffer.


system-wide keybinding setup:

best is a programable keyboard:


Emacs, Change Keys