Resetting a defvar Value

Apropos of yesterday’s post, I came across this query on Reddit and learned a new keystroke. One of the counterintuitive peculiarities of Elisp—and Lisps in general—is that a value defined by defvar or defcustom is immutable: once set it can’t be changed without restarting the process.

It turns out, though, that that’s not quite right. If you evaluate an expression such as

(defvar user "Jim")

with Ctrl+Meta+x instead of, say, Ctrl+x Ctrl+e you can change the value. I usually want to do this when I’m in the development phase an need to tweek a parameter. If you’re not an Elisp developer you won’t need to know this and even if you are, you probably won’t use it very often but it’s a really handy thing to know. And further proof that our Emacs education is never complete.

This entry was posted in General and tagged . Bookmark the permalink.
  • Phil

    I think "a value defined by defvar or defcustom is immutable" is a terribly misleading way of phrasing it. The value can be modified at any time! It's just that (re-)evaluating the form which defines it does not change the value if the variable is already bound.

    • jcs

      Yup. You're right, of course. I wrote hastily and misleadingly.

  • NoonianAtall

    Thanks, this is handy. This has bitten me several times while developing packages, and I end up having to change the defvars to setqs, and then change them back (at least Magit makes it easy to revert those hunks).