Question

For Emacs, how do I store what view-lossage collects into an external file? Ideally I'd like to store these keystroke data into an external log file incrementally and automatically, meaning it is done so by default when Emacs is started.

Was it helpful?

Solution

In Emacs 24 at least (I can't check a prior version right now), the docstring for view-lossage states:

Display last 300 input keystrokes.

To record all your input on a file, use `open-dribble-file'.

And C-hf open-dribble-file RET tells me:

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.

So simply add something like the following to your .emacs file:

(open-dribble-file (expand-file-name "~/.emacs.d/lossage.txt"))

Experimentally this clobbers the file if it already exists, so you'll need to deal with that.

Here's one approach. It accounts for multiple Emacs sessions by using make-temp-name to generate a semi-random filename for the dribble file, and then appends the contents of that to a primary lossage log file when Emacs exists. (If Emacs crashes, it would leave behind the temp file for you to deal with manually.)

(defmacro my-persistent-dribble-file (file)
  "Append the dribble-file for this session to persistent lossage log FILE."
  `(let* ((persistent-file (expand-file-name ,file))
          (temporary-file (make-temp-name (concat persistent-file "-")))
          (persistent-arg (shell-quote-argument persistent-file))
          (temporary-arg (shell-quote-argument temporary-file))
          (append-dribble-command (format
                                   "cat %s >>%s && rm %s"
                                   temporary-arg persistent-arg temporary-arg)))
     (open-dribble-file temporary-file)
     (eval `(add-hook 'kill-emacs-hook
                      (lambda () (shell-command ,append-dribble-command))))))

(my-persistent-dribble-file "~/.emacs.d/lossage")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top