50.2.1 Examining and Setting Variables

C-h v var RET

Display the value and documentation of variable var (describe-variable).

M-x set-variable RET var RET value RET

Change the value of variable var to value.

To examine the value of a variable, use C-h v (describe-variable). This reads a variable name using the minibuffer, with completion, and displays both the value and the documentation of the variable. For example,

C-h v fill-column RET

displays something like this:

fill-column is a variable defined in ‘C source code’.
Its value is 70

  Automatically becomes buffer-local when set.
  This variable is safe as a file local variable if its value
  satisfies the predicate ‘integerp’.
  Probably introduced at or before Emacs version 18.

Column beyond which automatic line-wrapping should happen.
Interactively, you can set the buffer local value using C-x f.

You can customize this variable.

The line that says ‘You can customize the variable’ indicates that this variable is a user option. C-h v is not restricted to user options; it allows non-customizable variables too.

The most convenient way to set a specific customizable variable is with M-x set-variable. This reads the variable name with the minibuffer (with completion), and then reads a Lisp expression for the new value using the minibuffer a second time (you can insert the old value into the minibuffer for editing via M-n). For example,

M-x set-variable RET fill-column RET 75 RET

sets fill-column to 75.

M-x set-variable is limited to customizable variables, but you can set any variable with a Lisp expression like this:

(setq fill-column 75)

To execute such an expression, type M-: (eval-expression) and enter the expression in the minibuffer (see Evaluating Emacs Lisp Expressions). Alternatively, go to the *scratch* buffer, type in the expression, and then type C-j (see Lisp Interaction Buffers).

Setting variables, like all means of customizing Emacs except where otherwise stated, affects only the current Emacs session. The only way to alter the variable in future sessions is to put something in your initialization file (see The Emacs Initialization File).

If you’re setting a customizable variable in your initialization file, and you don’t want to use the Customize interface, you can use the setopt macro. For instance:

(setopt fill-column 75)

This works the same as setq, but if the variable has any special setter functions, they will be run automatically when using setopt. You can also use setopt on other, non-customizable variables, but this is less efficient than using setq.