In emacs, you can create any keyboard shortcut to any command. This page shows you how.
For example, if you want 【F9】 for
calendar, then, place this code
(global-set-key (kbd "<f9>") 'calendar) in your emacs init file and restart emacs.
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 call
eval-last-sexp 【Ctrl+x Ctrl+e】. The new key will be active right away.
If you made some mistake and need to start emacs without loading your init files, you can start emacs on the command line like this:
(global-set-key (kbd "M-a") 'backward-char) ; Alt+a (global-set-key (kbd "C-a") 'backward-char) ; Ctrl+a
More at: Emacs Keybinding Syntax Examples.
How to find key syntax?
describe-key 【F1 k】, then press the key you want. Emacs will then display its syntax.
For example, suppose you want to know the syntax for the key press of 【Ctrl+Alt+F8】. Call
describe-key, then press 【Ctrl+Alt+F8】, then emacs will print
“<C-M-f8> is undefined”. That means, you can use
(kbd "<C-M-f8>") to represent that key combination in lisp code.
For examples, see: Emacs Keybinding Syntax Examples.
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: Emacs's Key Notations Explained (/r, ^M, C-m, RET, <return>, M-, meta).
What type of keys can i define?
What's the “Meta” key?
The Meta key is a key on Lisp Machine keyboards in the 1980s. 〔☛ Photo of LISP Keyboards〕 The Meta key doesn't exist in today's keyboards, but emacs remap it to PC keyboard's Alt key by default. When you see “Meta” mentioned in emacs, just think of it as “Alt”.
on the Mac, it's either ⌥ Opt or ⌘ Cmd, depending on which emacs distribution you are using. There's usually a menu that lets you chose.
How to unset a keybinding?
To unset a keybinding, set it to
nil, or use
;; unset a key (global-unset-key (kbd "C-b")) ;; or (global-set-key (kbd "C-b") nil)
How to find out the current keybinding to a key?
describe-key 【F1 k】, then type the key combination.
Emacs will then show the command that key press is bound to.
To see a list of ALL current keybindings, call
describe-bindings 【F1 b】.
How to swap Caps Lock and Control key?
You cannot do it within emacs, because these are at the OS level. See:
Emacs has its quirks. The following keys you should not redefine:
Emacs has some 7 thousand commands. By default, 800 of them have key shortcuts. 〔☛ A Curious Look at GNU Emacs's 1000+ Default Keybinding〕 All the common key spots are used. If you define your own keys without care, you may find that many major mode or minor mode override your keys, because they have priority.
By official emacs documentation (info "Key Binding Conventions") , the key space for users are the function keys F5 to F9, and 【Ctrl+c ‹letter›】. This is very restrictive.
The following keys are good spots for your own definitions, and does not cause any problems in practice.
|F5, F6, F7, F8, F9, F11, F12||Excellent|
|F1, F2, F3, F4, F10||Good if you don't use their defaults actions.|
|【Ctrl+F1】 to 【Ctrl+F12】||Excellent (be sure they are not used by the OS)|
|【Alt+F1】 to 【Alt+F12】||Excellent. (be sure they are not used by the OS)|
|【⇧ Shift+F1】 to 【⇧ Shift+F12】||Excellent|
|【Ctrl+0】 to 【Ctrl+9】, 【Alt+0】 to 【Alt+9】||Excellent, if you don't use their default action. By default they are |
|Keys on number pad, with or without a modifier||Very useful, but depending on which emacs distro/OS you are using, or terminal vs GUI, binding these keys may not work. Same thing can be said for those Insert, Delete, Home, End, Page Up, Page Down keys.|
|Hyper or ❖ Super||Any combination with these is good. You can set them to ❖ Win or ▤ Menu or ⌥ Opt. 〔☛ How to Define Super ＆ Hyper Keys〕|
A keypress combination such as 【Meta+⇧ Shift+2】 can also be
considered as 【Meta+@】. So, in emacs, you might be thinking that both of
(kbd "M-@")} works. Actually,
only the latter will work.
When writing a keybinding definition, for a key combination that
is 【Meta+⇧ Shift+‹key›】, you must use a code without the
-S if possible. But for key combination that is 【Ctrl+⇧ Shift+‹key›】, you must use the
A easy way to find out the proper syntax, is to call
describe-key 【F1 k】, then type the keystroke.
Note also, that keys involving 【Ctrl+⇧ Shift+‹key›】 cannot be distinguished from 【Ctrl+‹key›】 when emacs runs in a text terminal (telnet/ssh).
Example of shortcut with a punctuation key that are typed with Shift:
(global-set-key (kbd "C-:") 'backward-char) ; Ctrl+Shift+; or Ctrl+: (global-set-key (kbd "C-\"") 'backward-char) ; 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>") 'backward-char) ; Meta+Shift+F1 (global-set-key (kbd "C-S-<kp-2>") 'backward-char) ; Ctrl+Shift+“numberic pad 2” (global-set-key (kbd "C-M-<up>") 'backward-char) ; Ctrl+Meta+↑
When there are more than one modifier keys, such as 【Ctrl+Alt+a】, the order of the modifier in the string does not matter. It is recommended that they be alphabetical. So, write
See: Emacs Custom Keybinding to Enhance Productivity.