I use Emacs for programming and also read mail in Gnus. My problem is that I'm typing fast, switching buffers and Windows etc, and too often accidentally hit some key that is bound to some command that screws everything up. And I have no idea what I did hit.

Could be harmless things like suddenly my summary buffer in Gnus start to line wrap. But also serious things that screws up the Gnus information about mailboxes and mails, and I have to spend hours to try find a backup of those files and get things to a reasonable state again.

So first the simple question, is there a way to let emacs produce a "command log"? I.e. I can go into a log file and see that I accidentally hit gnus-do-something-unwanted so I can more easily find out how to get back to where I was before the incident?

Now, is there a way to globally in Emacs set so that all commands but those I have specified gives a dialog Do you really want to..?. While I use Emacs most of the day I only use a subset of all commands. And maybe I could list them in a .emacs.what.i.use.el file so that any emacs command outside those would bring me a dialog? This way preventing the disaster caused by a single accidental keystroke in Gnus or other mode?

If not possible, maybe there is a way to unbind all keys beside those I really use in Gnus? Lik (gnus-unbind-all-keys-beside ....)? :)

  • 3
    just regarding your first question, try command view-lossage. It lists all your last keystrokes with explanation what it did. Commented Oct 20, 2020 at 13:17
  • 1
    I don't think there's a good answer to your second question. I have had the same issue, but I notice it declines over time. First, as your typing improves, you make fewer mistakes. Second, as you make the same typos repeatedly, you start noticing what is going on, making it easier to back out. Definitely get used to the common exit keys: C-g and q cancel many things, and C-/ (possibly repeated) undoes most text changes.
    – Tyler
    Commented Oct 20, 2020 at 13:37

1 Answer 1


You can see what you've done with C-h l (aka M-x view-lossage).

As for putting a kind of "safety fence", it should be fairly easy to do, by placing an appropriate function of pre-command-hook. You could start with something like:

(defvar my-fence-let-pass
    '(self-insert-command next-line previous-line right-char left-char
      handle-select-window handle-switch-frame my-fence-mode))

(defun my-fence--check ()
   ((memq this-command my-fence-let-pass) nil)
   ((y-or-n-p (format "Do you really want to `%s`? " this-command))
    (push this-command my-fence-let-pass))
   (t (error "Abort"))))

(define-minor-mode my-fence-mode
  "Add a safety fence around the known commands."
  :global t
  (if my-fence-mode
      (add-hook 'pre-command-hook #'my-fence--check)
    (remove-hook 'pre-command-hook #'my-fence--check)))

but you'd probably want to improve it so it saves the list of acceptable commands to file or something like that.

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.