Status-Detective-783 · 2022-03-17 17:47
(define-key global-map "\C-c j" 'org-capture)
and
(define-key global-map "C-c j" 'org-capture)
Alan_Shutko · 2022-03-17 19:00
The difference is that the first works, and the second doesn't. If you pass in a string, the Control key needs to be shown as `\C`. Otherwise, it thinks that the first key in the sequence is a capital "C".
However, the "C-c j" is very familiar to users, so there's a function called `kbd` which will convert that form to the string needed for define-key, and you would use it like
```
(define-key global-map (kbd "C-c j") 'org-capture)
```
thetemp_ · 2022-03-17 19:23
I assume the intent is to bind the sequence made by entering "C-c" (holding Ctrl while pressing "c") followed by the "j" key. But neither of these forms will do that.
In the first form, the "\C-c" is properly treated as Ctrl + "c", because the backslash escapes the "C-", causing it to be treated as a Ctrl-modifier applied to the "c" key. Besides that, however, each character in the string will be treated as a separate event. So it will be treated as a sequence of 3 keys, "C-c" then <space> then "j".
In the second one, there is no backslash, so each character will be treated as a its own key, resulting in a sequence of 5 keys: "C" then "-" then "c" then <space> then "j". To use that type of string, you can use it as the argument to the `kbd` function.
Here are 3 equivalent ways to bind "j" under the "C-c" prefix.
(define-key global-map "\C-cj" 'org-capture)
(define-key global-map (kbd "C-c j") 'org-capture)
(define-key global-map [?\C-c ?j] 'org-capture)
The last one is [preferred](https://www.gnu.org/software/emacs/manual/html_node/elisp/Strings-of-Events.html).
00-11 · 2022-03-17 20:46
No, the last one is not preferred. It's recommended over the first one; that's all.
Generally, the use of `kbd` is preferred. The closest the Emacs manual comes to a recommendation in favor of it is in node [Init Rebinding](https://www.gnu.org/software/emacs/manual/html_node/emacs/Init-Rebinding.html). But it says only *"The
simplest is to use the `kbd` function"*.
And the Elisp manual (node [Changing Key Bindings](https://www.gnu.org/software/emacs/manual/html_node/elisp/Changing-Key-Bindings.html)) says only this (also something less than an out-and-out recommendation): *"The ‘kbd’
function is a convenient way to specify the key
sequence."*
The reason `kbd` is generally preferable is that the syntax it accepts is the *same syntax that Emacs itself uses* when it talks to you about key sequences. (That's what function `key-description` returns.)
Status-Detective-783 · 2022-03-17 23:37
Ahh that's my confusion! Sometimes I saw the "C-c j" form, sometimes the "\C-c j" form. But I see now they are both used properly in my init.el file. Thanks for clearing that up!
Status-Detective-783 · 2022-03-17 23:38
Excellent, thanks!