6

Suppose, we have a variable defined in ~/.emacs:

(defvar *my-var* "")

Then in **scratch** buffer we set some value to it:

(setq *my-var* "value")

After we close emacs the value will be lost. How can we store the value to disk and read it again when Emacs is started?

Drew
  • 75,699
  • 9
  • 109
  • 225
user4035
  • 1,039
  • 11
  • 24

1 Answers1

5

desktop-save-mode will do this for you. It saves more than just the state of variables though, it also saves open buffers, window layouts and more.

To explicitly save the value of a global variable with you desktop, add the symbol to the list: desktop-globals-to-save. For local variables, use desktop-locals-to-save.

You can read more about desktop-save-mode here to learn about enabling and customizing it.


Alternatively, persisting your scratch buffer between sessions so you could reevaluate customizations may suffice for your example. persistent-scratch is available on MELPA and simplifies the process of saving your scratch buffer between sessions.

Jordon Biondo
  • 12,332
  • 2
  • 41
  • 62