As described here, by C-x z, I can repeat my last keystroke. How can I repeat my last n keystrokes?

  • 2
    I don't have an answer to this, but have you tried using keyboard macros for repeating a batch of commands? – Kaushal Modi Feb 18 '15 at 17:58

Emacs records the last 300 input events (mainly keystrokes, but also mouse clicks and such). In Elisp, you can access them by calling recent-keys. As a user, you can view the list of keystrokes by invoking the command view-lossage by pressing C-h l or f1 l.

Glancing through the uses of the recent-keys function, the only thing I can find in Emacs itself that would help you repeat N keystrokes is the macro editor facility. (I haven't looked at third-party packages.)

kmacro-edit-lossage (C-x C-k l) brings up those last 300 keystrokes in the macro editor. You can edit the buffer to remove the lines you don't want to repeat (press C-k to remove a line; press ESC 42 C-k to remove the next 42 lines; press C-SPC, move and press C-w to delete a region of the buffer). Then press C-c C-c to finish editing and C-x e to execute the macro.

  • This is good to know. But looks like if a mouse event is recorded as one of the last 300 events, then you get an error saying Macros with mouse clicks are not supported by this command on doing M-x kmacro-edit-lossage. – Kaushal Modi Feb 18 '15 at 22:23
  • Aside the problem raised by @kaushalmodi, the entries, given by kmacro-edit-lossage, seems to be static. Though I am for the moment unable to use your suggestion to settle the problem, I think your answer is helpful and very interesting. – Name Feb 19 '15 at 5:17
  • @Name I don't understand what you mean by “the entries seems to be static”. Regarding mouse events, the lossage data is as good as it gets unless you activate some additional recording (I think there are packages for that) — Emacs records events, not actions, and I think that for mouse events it only records “click at this position” and not “click on this object” (which would be hard because the “object”, e.g. an overlay, might not exist anymore). It would be easier if you started recording a macro (C-x () before the actions that you want to repeat. – Gilles 'SO- stop being evil' Feb 19 '15 at 8:49
  • So let me ask if with this method you can write a macro equivalent to C-x z, i.e., the case n=1. – Name Feb 19 '15 at 10:23
  • 1
    @kaushalmodi About the mouse events problem, I drafted a replacement for kmacro-edit-lossage in this question: emacs.stackexchange.com/a/4071/184 As a failsafe, it will only return the keys since the last mouse event, but keys prior to that are probably meaningless without the mouse event anyway. – T. Verron Feb 19 '15 at 13:47

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.