20

Is there a place where emacs stores commands the user has recently executed, in particular via keyboard shortcuts, and if not, would it be possible to tell it to store them somewhere? I sometimes type the wrong keyboard shortcut by accident and I don't know what it is that I did. I undo it, but I tend to repeat these accidental shortcuts every once in a while, and I have no idea how to find out what exactly I pressed or what command I ran.

I know how to determine what command is bound to a key. My issue is that I know neither the command nor the keys I pressed, only what effect they had and the fact that I recently did it.

Zorgoth
  • 810
  • 6
  • 14
  • 1
    Although some functions expressly set `last-command` to something special, for the most part you can just check the value of that variable to see the most recent last command. – lawlist Jul 28 '17 at 00:42

2 Answers2

22

When some key sequence triggers an unexpected command, use view-lossage (bound to C-h l by default) to see what keystrokes Emacs has recently received. This is most useful since Emacs 25 as it now also shows the commands invoked by each key sequence. In earlier releases you'll just see the raw keystrokes.

glucas
  • 20,175
  • 1
  • 51
  • 83
  • 1
    The **worst** part is that the keystrokes `C-h l` (or worse M-x view-lossage) are included in this short list of recent-keys. So you better type that **as soon as** you notice the wierdness – Bae Oct 13 '20 at 01:41
11

As @glucas has mentioned, view-lossage helps. Unfortunately, it displays relatively few events, and users have no control over the number.

As C-h k C-h l tells you, you can record all keyboard characters by using open-dribble-file:

view-lossage is an interactive compiled Lisp function in help.el.

It is bound to C-h l, <f1> l, <help> l.

(view-lossage)

Display last few input keystrokes and the commands run.

To record all your input, use open-dribble-file.


open-dribble-file is an interactive built-in function in C source code.

(open-dribble-file FILE)

Start writing all keyboard characters to a dribble file called FILE.

If FILE is nil, close any open dribble file. The file will be closed when Emacs exits.

Be aware that this records ALL characters you type! This may include sensitive information such as passwords.

Drew
  • 75,699
  • 9
  • 109
  • 225