First I define a global variable x
:
ELISP> (defvar x (cons 1 3))
x
ELISP> x
(1 . 3)
After some operations, I want to shadow x
and reassign its value to 5:
ELISP> (defvar x 5)
x
ELISP> x
(1 . 3)
But it still has the original value. How can I shadow a global variable?
The defvar
form does not override the value if the variable is already defined. This is so that the user can say (setq preference value)
in their .emacs
even before loading the package which declares and uses preference
using defvar
.
As suggested above, use setq
to permanently and unconditionally replace any previous value.
To temporarily override a value, use let
.
(let ((preference temp-value))
... code which needs to see temp-value ...)
;; previous global value is restored after the let form