Emacs Keys: Define Key
How to Assign Key to Command
In emacs, you can create any keyboard shortcut to any command or a Key Macro.
For Emacs 29 (Released 2023-07) , the syntax to change key is this
(keymap-global-set key cmd)
The key is a string of the Key Syntax .
The cmd can be a command name (a Symbol), or a string of Key Macro syntax.
Simply type that code in any buffer, and eval it. The new keybinding is effective immediately. 〔see Emacs: Evaluate Elisp Code〕
Or, you can put the code in Emacs: Init File. If you made some mistake and need to start emacs without loading your init file, see Emacs Init: Ignore Init File.
Example, Emacs 29 or Later
Define a Key for a Command
;; emacs 29 syntax ;; make Ctrl+t call whitespace-mode (keymap-global-set "C-t" #'whitespace-mode)
Define a Key for Key Macro
;; example for emacs 29, make the f6 key send control-g (keymap-global-set "<f6>" "C-g")
;; example for emacs 29, make the f6 key insert a string. space between chars are required (keymap-global-set "s o m e t h i n g")
Example, Emacs 28 or before
for emacs 28 or before, see
;; emacs 28 syntax (global-set-key (kbd "C-t") #'whitespace-mode)
Remove a Keybinding
Emacs 29
;; remove a keybinding ;; for emacs 29 or after (keymap-global-set "C-t" nil) ;; or use (keymap-global-unset "C-t")
Emacs 28
;; before emacs 28 or before (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 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:
Reference
Emacs, Change Keys
- Emacs Keys: Define Key
- Emacs Keys: Keybinding Functions (emacs 29 and emacs 28)
- Emacs Keys: Syntax
- Emacs Keys: Good and Bad Key Choices
- Emacs Keys: Swap CapsLock Control
- Emacs Keys: Meta Key
- Emacs Keys: Change Major Mode Keys
- Emacs Keys: Change Minor Mode Keys
- Emacs Keys: Minor Modes Key Priority
- Emacs Keys: Change Minibuffer Keys