Defining Global Variables
A variable definition is a construct that announces your intention to use a symbol as a global variable. It uses the special forms defvar or defconst, which are documented below.
A variable definition serves three purposes. First, it informs people who read the code that the symbol is intended to be used a certain way (as a variable). Second, it informs the Lisp system of this, optionally supplying an initial value and a documentation string. Third, it provides information to programming tools such as etags, allowing them to find where the variable was defined.
The difference between defconst and defvar is mainly a matter of intent, serving to inform human readers of whether the value should ever change. Emacs Lisp does not actually prevent you from changing the value of a variable defined with defconst. One notable difference between the two forms is that defconst unconditionally initializes the variable, whereas defvar initializes it only if it is originally void.
To define a customizable variable, you should use defcustom (which calls defvar as a subroutine). See Defining Customization Variables.
- Special Form: defvar symbol [value [doc-string]]
-
This special form defines symbol as a variable and optionally initializes and documents it. Note that it doesn’t evaluate symbol; the symbol to be defined should appear explicitly in the
defvarform.defvaralso marks symbol as special, meaning that its bindings should always be dynamic (see Scoping Rules for Variable Bindings).If value is specified, and symbol is void (i.e., it has no dynamically bound value; see When a Variable is Void), then
defvarevaluates value, and initializes symbol by setting it to the result of the evaluation. But if symbol is not void,defvardoes not evaluate value, and leaves symbol’s value unchanged. If value is omitted,defvardoesn’t change the value of symbol in any case.Note that specifying a value, even
nil, marks the variable as special permanently. Whereas if value is omitted, thendefvarmarks the variable special only locally (i.e. within the current lexical scope, or within the current file, ifdefvaris at the top-level). This can be useful for suppressing byte compilation warnings, see Compiler Errors.If symbol has a buffer-local binding in the current buffer, and value is specified,
defvarmodifies the default value of symbol, which is buffer-independent, rather than the buffer-local binding. It sets the default value if the default value is void. See Buffer-Local Variables.If symbol is already let bound (e.g., if the
defvarform occurs in aletform), thendefvarsets the toplevel default value of symbol, likeset-default-toplevel-value. The let binding remains in effect until its binding construct exits. See Scoping Rules for Variable Bindings.When you evaluate a top-level
defvarform with C-M-x (eval-defun) or with C-x C-e (eval-last-sexp) in Emacs Lisp mode, a special feature of these two commands arranges to set the variable unconditionally, without testing whether its value is void.If the doc-string argument is supplied, it specifies the documentation string for the variable (stored in the symbol’s
variable-documentationproperty). See Documentation.Here are some examples. This form defines
foobut does not initialize it:(defvar foo) ⇒ fooThis example initializes the value of
barto23, and gives it a documentation string:(defvar bar 23 "The normal weight of a bar.") ⇒ barThe
defvarform returns symbol, but it is normally used at top level in a file where its value does not matter.For a more elaborate example of using
defvarwithout a value, see Local defvar example.
- Special Form: defconst symbol value [doc-string]
-
This special form defines symbol as a value and initializes it. It informs a person reading your code that symbol has a standard global value, established here, that should not be changed by the user or by other programs. Note that symbol is not evaluated; the symbol to be defined must appear explicitly in the
defconst.The
defconstform, likedefvar, marks the variable as special, meaning that it should always be dynamically bound (see Scoping Rules for Variable Bindings). In addition, it marks the variable as risky (see File Local Variables).defconstalways evaluates value, and sets the value of symbol to the result. If symbol does have a buffer-local binding in the current buffer,defconstsets the default value, not the buffer-local value. (But you should not be making buffer-local bindings for a symbol that is defined withdefconst.)An example of the use of
defconstis Emacs’s definition offloat-pi—the mathematical constant pi, which ought not to be changed by anyone (attempts by the Indiana State Legislature notwithstanding). As the second form illustrates, however,defconstis only advisory.(defconst float-pi 3.141592653589793 "The value of Pi.") ⇒ float-pi(setq float-pi 3) ⇒ float-pifloat-pi ⇒ 3
Copyright © 1990-1996, 1998-2026 Free Software Foundation, Inc.
Licensed under the GNU GPL license.
https://www.gnu.org/software/emacs/manual/html_node/elisp/Defining-Variables.html