4 ms·
What I would really like is an Emacs macro that would just insert the appropriate keybinding code for use in an .emacs file -- there doesn't seem to be one but
by pangram 16y ago
What I would really like is an Emacs macro that would just insert the appropriate keybinding code for use in an .emacs file -- there doesn't seem to be one but it seems like it would be an obvious help. Or is this one of those Emacs things that is obvious and everyone knows but me?
- silentbicycle 16y agoThe simplest way I've found to format the keyboard shortcuts is to type "C-h k [your keyboard combination]" (which is C-h for help, k for keybinding), and then when it says e.g. "C-x M-. is undefined", plug the key description into the following: (global-set-key (kbd "keybinding") 'function-you-want-it-to-call) The article goes into the significance of global-set-key vs. local-set-key; until you have more specific intentions, global-set-key is probably what you want. I've done a couple sessions teaching developers in using Emacs, and one point I always stress is that the Emacs help system is exhaustive, but you have to learn how it's organized. (It uses funny terminology sometimes.)
- funptr 16y agoThis one prompts you to type a literal key sequence (exactly like with `describe-key', C-h k) and inserts it at the point in a representation that the *-set-key functions can use. As written it's bound to to C-c k, and only in the scope of elisp buffers. (defun insert-key-sequence (key-repr) "Reads a literal key sequence from the user and inserts a representation of it at point, suitable for `global-set-key' or `local-set-key'." (interactive "KKey sequence? ") (prin1 key-repr (current-buffer)) (insert " ")) (add-hook 'emacs-lisp-mode-hook #'(lambda () (local-set-key "\C-ck" #'insert-key-sequence))) (run-hooks 'emacs-lisp-mode-hook) Unfortunately it doesn't seem to work for mouse chords.
- silentbicycle 16y agoNice. I tried to write a quick version of this for my response, but wasn't aware of insert-key-sequence. Edit: insert-key-sequence doesn't exist on Emacs 22.3.1, is that a more recent addition? Also, while not crucial, I prefer to add a my-X-mode-hook function and add things to that, rather than adding several lambdas to the mode hook - it's easier to modify them all after binding that way.
- deleted 16y ago[deleted]
- funptr 16y agoIt's not part of emacs, I just wrote it as an example.
- silentbicycle 16y agoOh, right. Sorry, hadn't had my coffee yet. I tried doing exactly that, but used (interactive "kKey: "), with lowercase k, and it didn't work.
- funptr 16y agoThat seems to work also, what's the problem?
- silentbicycle 16y agoDidn't work correctly with a few slightly-more elaborate key combinations I tried. IIRC C-x M-. was one of them. I much prefer formatting the keybindings like kbd uses, anyway - do you know offhand how to convert a keypress to that format w/out using C-h k?
- funptr 16y agoNot offhand, but I looked it up and I think what you want is `key-description'. It prints key sequences, so e.g. (key-description "\370") evals to "M-x".
- silentbicycle 16y agoBingo. Thanks. This should do what the original commenter wanted: (defun insert-keybinding (k) "Insert code for a keybinding." (interactive "kKey sequence: ") (insert (concat "(global-set-key (kbd \"" (key-description k) "\") 'function-name)")))
- deleted 16y ago[deleted]