Reset a variable to its default value in Emacs Lisp

Emacs Lisp provides set to set a variable globally:1

(set 'org-html-doctype "html5")

To get its standard value, as defined in its variable definition, define a function named jk/default:2, 3

(defun jk/default (variable)
  (eval (car (get 'org-html-doctype 'standard-value))))

Calling the default function returns a variable’s standard value:

(jk/default 'org-html-doctype)

To reset the variable, define a function named jk/reset that calls set with the standard value:

(defun jk/reset (variable)
  (set variable (jk/default variable)))

Using the reset function, a variable can be set, and then reset to its original value:

(set 'org-html-doctype "html5")
(jk/reset 'org-html-doctype)

To temporatily reset a variable to its default value, use the default function in a let expression:

(set 'org-html-doctype "html5")

(let ((org-html-doctype (jk/default 'org-html-doctype)))

  1. The setq function (for “set quoted”) is a convenience function that assumes the first argument should be a quoted value. The following examples are equivalent:

    (set 'org-html-doctype "html5")
    (setq org-html-doctype "html5")
  2. ↩︎
  3. The function name is prefixed to prevent unintentionally overwriting (future) built-in functions.