20

I'm bored of looking up key names for the kbd function, is there a way to automatically insert the correct key name just by pressing the key?

So for example I want something to do this: I'm typing

(local-set-key |

I run magical-insert-kbd-command, press f6 and I get

(local-set-key (kbd "<f6>") |
20

Not sure exactly what you are asking. But C-h k followed by hitting the key shows you exactly what string you need to pass to kbd. For example, C-h k f6 shows you <f6>.

Here is a command that does what you want, I guess and binds the result to C-c c:

(defun foo (key)
  (interactive "kKey: ")
  (insert (format "(kbd %S)" (key-description key))))

(global-set-key (kbd "C-c c") 'foo)

Then type:

(local-set-key C-c c

That prompts you to use a key. If you hit the F6 key then you get this:

(local-set-key (kbd "<f6>") 

[Updated per suggestion by @HaraldHancheOlsen: Changed "(kbd \"%s\")" to ".](kbd %S)"

  • Perfect, thanks!. The main thing I was missing was missing the key-description function. – dshepherd Jun 4 '15 at 21:56
  • But OP is right that it would be a useful to have a function that automatically placed the keybinding in the kill ring. – incandescentman Jun 27 '15 at 6:44
  • 1
    @incandescentman: Huh? Where does the OP say anything about the kill ring? And just what do you mean by the "keybinding" in that sentence? A key binding is not a string, which is what the kill-ring contains. Do you mean the key-description string? or the return value of the above format sexp? Anyway, it should be clear how to modify the command I gave, to add either of those to the kill-ring (just use kill-new). – Drew Jun 27 '15 at 15:48
  • "automatically insert." If I understand correctly, the solution above only displays the keybinding (e.g. kbd <M-return>) but does not insert it into an init file. (You'd have to do the additional step of copy/paste.) Maybe I misunderstood. For my own personal use case, I would find it useful to have a way to query the syntax for a keybinding (e.g. is the syntax M-RET or M-<return> or what?) and automatically place that string in the kill ring/pasteboard, so that I could then paste the keybinding into my init file when writing a line like (define-key map (kbd "<M-return>") 'org-return). – incandescentman Jun 27 '15 at 20:46
  • 1
    Yes, you misunderstood. (But there was also a typo in the key-binding sexp: I forgot to use kbd in the global-set-key sexp - corrected now.) Command foo (which the global-set-key sexp binds to C-c c) reads a key sequence and then inserts, at point of the current buffer, the proper kbd sexp for it. If you use C-c c in your init file, after (local-set-key , then it inserts the kbd sexp you need for the key it reads. – Drew Jun 27 '15 at 20:46
7

If you use M-x local-set-key to do your keybinding interactively, you can use C-x ESC ESC (repeat-complex-command) to get a Lisp expression that you can paste into your config. It probably won't use friendly (kbd ...)-style descriptions, though.

  • Neat. I tend to use define-key though actually, it seems simpler to just define the key outright than to set up a hook to call a function to define the key. – dshepherd Jun 5 '15 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.