1

I would like to write a command that defines key bindings for me, so that I can feed in key y (for example) and have it define key bindings for M-y, C-y etc.

I'm trying to concat the command argument into a string, but it gives me the error Invalid modifier in string.

(defun my/keypress-definer (keypress)
  (interactive)
     (concat "\C-" keypress))

(my/keypress-definer "a")

Is there any way to achieve this?

2

The concat function takes a list of SEQs and returns a string.

To get the string that describes your key sequence, you would simply write

(concat "C-" keypress)

Use the kbd function to return the actual key representation. I think you may end up with something like the following

(defun my/keypress-definer (keypress)
  (define-key my-key-map
    (kbd (concat "C-" keypress)) 'my/ctl-function)
  (define-key my-key-map
    (kbd (concat "M-" keypress)) 'my/meta-function))

Documentation:

 - [Creating Strings][1]
 - [Key Sequences][2]

  [1]: https://www.gnu.org/software/emacs/manual/html_node/elisp/Creating-Strings.html
  [2]: https://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Sequences.html
| improve this answer | |
  • 1
    It works, it works, it works, it works! You are the absolutely best type of person, thank you. :) – randouser Oct 30 at 3:48
  • ok - I'm not going to stop answering questions. You made my day! – gregoryg Oct 31 at 3:20

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.