Separate CIDER REPL Histories for Different Clojure Projects



CIDER doesn't let me have different histories for different projects! Oh, yes it does.

Update 2019-10-08

There's a simpler approach than the one explained in the original version of this article.

I'm successfully using this simpler approach with CIDER 0.22.1-snapshot. Given comments I've seen from CIDER developers and others in the past, I think earlier versions of CIDER might have worked differently. I don't know when this approach started to work. Perhaps it has always worked.

You can simply (globally) set cider-repl-history-file to a filename such as ".cider-repl-history", and CIDER will use a file of that name in each project's root directory.

This is possible because CIDER sets default-directory to the project root before using the value of cider-repl-history-file.

The original article follows…

CIDER provides a REPL history that persists across REPL sessions. You set this up by setting the (Emacs) variable cider-repl-history-file to be the name of the file in which to store the history. CIDER will then read from the file when starting up a REPL and write to it when closing down a REPL.

History is Shared Across Projects    

That single variable is shared by all projects, which means you have one history that is shared by all projects. (FWIW, there is some jiggery-pokery that means if you have more than one REPL running at the same time you don't lose history as a result of one project blatting the write of another project.)

Annoying    

The single shared history is good if you are just playing around with stuff that isn't project-specific, but mostly I find it annoying — I want separate REPL histories for different Clojure projects.

For a while I hacked CIDER each time I upgraded it so that it created a separate file for each project. But then something changed in the CIDER code that made it harder to apply my hack and I stopped bothering.

So for a good while now I've had a single CIDER history shared across all projects.

Separate Histories for Different Projects    

But finally I've found a way to fix this, mentioned in a comment about Emacs .dir-locals on a CIDER issue about persistent REPL history. A little digging led me to the following solution…

Simply create a file called .dir-locals.el in your home directory with this content:

((cider-repl-mode
  (cider-repl-history-file . ".cider-repl-history")))

This will create a directory-local variable for cider-repl-history-file in cider-repl-mode, and each of your Clojure projects will have its own history file.

As another commenter on that CIDER issue said, Now, that sounds emacsy.