0

Sometimes I wish to persist the value of some variable when I quit emacs. For example, I would like to do this for compile-command.

Now, I guess I could find the hook that is run when Emacs quits (assuming there is one, a quick scan through the list of hooks did not turn up an obvious candidate) and save that variable to a file of my own; and reload it in init.el. But surely someone else had the same need already. Is there a package or even some inbuilt way to achieve this?

Bonus question: it would be awesome to also be able to save the history of the same. I.e., when I type M-x compile, I can scroll back through the previous compile commands. Would be great to keep that. Same for M-! with shell commands and so on.

Drew
  • 75,699
  • 9
  • 109
  • 225
AnoE
  • 356
  • 1
  • 11
  • It sounds like file-local or directory-local variables might serve in the specific use case described here. – John Kitchin Feb 22 '21 at 12:34
  • @JohnKitchin yes, especialy directory-local would serve for my `compile` related stuff; thanks for bringing that up. I'll use those if no good answers turn up. – AnoE Feb 22 '21 at 12:37
  • Yes, @drew, that seems like a direct duplicate. Didn't use the correct search terms, looks like. – AnoE Feb 23 '21 at 10:54
  • Please delete the question, in that case. Thx. – Drew Feb 23 '21 at 15:50
  • 1
    Well, since I was not finding that other question, I'll keep this on. The other one is specific to two use cases; this one is generic for all kinds of variables. Yes, the answers are similar or the same, but since I at least did not find the other, this one might be practical for other people searching for "my" search terms... @Drew – AnoE Feb 24 '21 at 09:05

1 Answers1

4

Is there ... some inbuilt way to achieve this?

Yes -- if you enable savehist-mode in your init file, then any variable you add to savehist-additional-variables will automatically be saved and restored between sessions.

Bonus question: it would be awesome to also be able to save the history of the same

savehist-mode automatically saves most history variables, but this one is a bit different, so you'll need to treat it the same way.

Try this:

(savehist-mode 1)
(add-to-list 'savehist-additional-variables 'compile-command)
(add-to-list 'savehist-additional-variables 'compile-history)
phils
  • 48,657
  • 3
  • 76
  • 115
  • Awesome, exactly as ordered! ;) I also added `compilation-directory` and `shell-command-history`. – AnoE Feb 22 '21 at 12:43