How to get recentf-mode to work with emacs server/client

emacs

I like using recentf mode in Emacs which produces a list of files I have recently opened. This list is persistent between sessions, so I can get to files I used a fairly long time ago.

However, I recently started using Emacsclient to connect to an Emacs server which is automatically turned on when I log it. Overall, this was a big improvement. However, recentf mode started missing most of the files I opened whenever I restarted my computer. It remembers some of them, and I cannot see a pattern in which files get recorded.

How can I fix this behavior? How can I get it to record all the files I visit and remember them between sessions?

Best Answer

I was having this exact same problem, and I solved it by making two changes. First following this post, I created a function to automatically invoke recentf-save-list on a timer. Thus every few minutes, we automatically re-write the ~/.recentf file:

(run-at-time nil (* 5 60) 'recentf-save-list)

The other thing I did was to add a shutdown script to my display manager (lightdm) that gracefully closed the emacs server every time I logout or shutdown. The way you do this will depend on your display manager, but my script simply looks like

#!/bin/bash
emacsclient -e "(save-buffers-kill-emacs)"

It would also be nice to have a hook that automatically re-ran recentf-save-list every time a client "disconnected" from the server, but I couldn't figure out the proper hooks.