I have a command that I would like to bind to multiple key sequences differing only in the final decimal digit (e.g. C-. 0, C-. 1, ..., C-. 9). The command needs to determine that final digit.

I would prefer not to have 10 distinct bindings, each passing a distinct argument to a shared implementation. So I am wondering whether emacs keeps around the event sequence that triggers a command. Surely the self-insert-command has such a capability (though of course it is written in C).

  • 1
    Looks like you actually want to have a command with a numeric argument, i.e., you want to bind C-. to a command with, e.g., (interactive "e") as first form. Try the help for interactive for alternatives. (You could also use a numeric prefix argument.)
    – Tobias
    Aug 24, 2015 at 4:44
  • As initially described perhaps. That is not possible in my case because these bindings are part of a larger scheme: "C-. m 0", "C-. m 1", ..., "C-. m 9" and "C-. x 0", C-. x 1", ... "C-. x 9".
    – John Yates
    Aug 24, 2015 at 11:58

2 Answers 2


C-h f this-command-keys:

this-command-keys is a built-in function in `C source code'.


For more information check the manuals.

Return the key sequence that invoked this command. However, if the command has called read-key-sequence, it returns the last key sequence that has been read. The value is a string or a vector.

See also this-command-keys-vector.

See the Emacs manual, node Command Loop Info.

  • Thanks Drew. That is exactly the suggestion I needed. (I am not surprised that you were the one to provide the proper answer :-).
    – John Yates
    Aug 24, 2015 at 12:00
  • If it answers the question then consider accepting it. Having an accepted answer can help other users learn from the Q & A.
    – Drew
    Jan 14, 2019 at 3:55

self-insert-command is indeed written in C but could just as well be written in Elisp (it's just a result of historical evolution). The variable it uses to figure out which key was pressed is last-command-event.

  • Stefan, The documentation on last-command-event is rather sparse. I assume that in a mult-key sequence last-command-event contains only the final key. Is that correct? It is simply an integer? To check for a modified key would I write something like
    – John Yates
    Aug 24, 2015 at 19:25
  • (= last-command-event (kbd "C-["))
    – John Yates
    Aug 24, 2015 at 19:32
  • Yes, an "event" is a single key press (possibliy with modifiers). It can be an integer (a character, possibly with modifiers) or a symbol, or a structured event (e.g. a moue-click). For your example, the value would be ?\C-\[ which is the integer 27. (kbd "C-[") is not quite right, since kbd handle key sequences, i.e. sequences of events rather than single events. IOW, (aref (kbd "C-[") 0) would give you the right answer. There are various functions to extract data from events; try C-h f event- TAB to see a list of them.
    – Stefan
    Aug 25, 2015 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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