5

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

Name
  • 7,689
  • 4
  • 38
  • 84
  • 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
  • It's year 2023 and there is no answer to your question yet there. Have you in between found a way to accomplish what you have asked for? If yes, would you be so kind to provide it as own answer to own question? – Claudio Apr 02 '23 at 16:36

1 Answers1

6

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 05: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 08: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
  • @Name IMO the [keyboard macro recording](https://www.gnu.org/software/emacs/manual/html_node/emacs/Basic-Keyboard-Macro.html) is the best way to go. It would also help to know what your use case is. Why do you want to record last n key strokes? – Kaushal Modi Feb 19 '15 at 11:42
  • 1
    @kaushalmodi About the *mouse events* problem, I drafted a replacement for `kmacro-edit-lossage` in this question: http://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