GNU Emacs
Emacs
Dashboard

49.2.3 Local Variables

M-x make-local-variable RET var RET

Make variable var have a local value in the current buffer.

M-x kill-local-variable RET var RET

Make variable var use its global value in the current buffer.

M-x make-variable-buffer-local RET var RET

Mark variable var so that setting it will make it local to the buffer that is current at that time.

Almost any variable can be made local to a specific Emacs buffer. This means that its value in that buffer is independent of its value in other buffers. A few variables are always local in every buffer. Every other Emacs variable has a global value which is in effect in all buffers that have not made the variable local.

M-x make-local-variable reads the name of a variable and makes it local to the current buffer. Changing its value subsequently in this buffer will not affect others, and changes in its global value will not affect this buffer.

M-x make-variable-buffer-local marks a variable so it will become local automatically whenever it is set. More precisely, once a variable has been marked in this way, the usual ways of setting the variable automatically do make-local-variable first. We call such variables per-buffer variables. Many variables in Emacs are normally per-buffer; the variable’s document string tells you when this is so. A per-buffer variable’s global value is normally never effective in any buffer, but it still has a meaning: it is the initial value of the variable for each new buffer.

Major modes (see Major Modes) always make variables local to the buffer before setting the variables. This is why changing major modes in one buffer has no effect on other buffers. Minor modes also work by setting variables—normally, each minor mode has one controlling variable which is non- nil when the mode is enabled (see Minor Modes). For many minor modes, the controlling variable is per buffer, and thus always buffer-local. Otherwise, you can make it local in a specific buffer like any other variable.

A few variables cannot be local to a buffer because they are always local to each display instead (see Multiple Displays). If you try to make one of these variables buffer-local, you’ll get an error message.

M-x kill-local-variable makes a specified variable cease to be local to the current buffer. The global value of the variable henceforth is in effect in this buffer. Setting the major mode kills all the local variables of the buffer except for a few variables specially marked as permanent locals.

To set the global value of a variable, regardless of whether the variable has a local value in the current buffer, you can use the Lisp construct setq-default. This construct is used just like setq, but it sets variables’ global values instead of their local values (if any). When the current buffer does have a local value, the new global value may not be visible until you switch to another buffer. Here is an example:

(setq-default fill-column 75)

setq-default is the only way to set the global value of a variable that has been marked with make-variable-buffer-local.

Lisp programs can use default-value to look at a variable’s default value. This function takes a symbol as argument and returns its default value. The argument is evaluated; usually you must quote it explicitly. For example, here’s how to obtain the default value of fill-column:

(default-value 'fill-column)