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?

  • Is there a reason you don't just add that line to the .emacs file?
    – user2699
    Dec 2, 2015 at 16:44
  • @user2699 In reality I don't know the value, that will be set to *my-var*
    – user4035
    Dec 2, 2015 at 18:38
  • This is the approach I use: stackoverflow.com/questions/2321904/…
    – Tyler
    Dec 2, 2015 at 19:20

1 Answer 1


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.

  • Is it possible to save without a mode? Like write the variable into file?
    – user4035
    Dec 2, 2015 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.