manpagez: man pages & more
info emacs
Home | html | info | man
[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ] Specifying File Variables

There are two ways to specify file local variable values: in the first line, or with a local variables list. Here's how to specify them in the first line:

-*- mode: modename; var: value; … -*-

You can specify any number of variables/value pairs in this way, each pair with a colon and semicolon as shown above. mode: modename; specifies the major mode; this should come first in the line. The values are not evaluated; they are used literally. Here is an example that specifies Lisp mode and sets two variables with numeric values:

;; -*- mode: Lisp; fill-column: 75; comment-column: 50; -*-

You can also specify the coding system for a file in this way: just specify a value for the “variable” named coding. The “value” must be a coding system name that Emacs recognizes. See section Coding Systems. ‘unibyte: t’ specifies unibyte loading for a particular Lisp file. See section Enabling Multibyte Characters.

The eval pseudo-variable, described below, can be specified in the first line as well.

In shell scripts, the first line is used to identify the script interpreter, so you cannot put any local variables there. To accommodate this, Emacs looks for local variable specifications in the second line when the first line specifies an interpreter.

A local variables list goes near the end of the file, in the last page. (It is often best to put it on a page by itself.) The local variables list starts with a line containing the string ‘Local Variables:’, and ends with a line containing the string ‘End:’. In between come the variable names and values, one set per line, as ‘variable: value’. The values are not evaluated; they are used literally. If a file has both a local variables list and a ‘-*-’ line, Emacs processes everything in the ‘-*-’ line first, and everything in the local variables list afterward.

Here is an example of a local variables list:

;; Local Variables: **
;; mode:lisp **
;; comment-column:0 **
;; comment-start: ";; "  **
;; comment-end:"**" **
;; End: **

Each line starts with the prefix ‘;; ’ and each line ends with the suffix ‘ **’. Emacs recognizes these as the prefix and suffix based on the first line of the list, by finding them surrounding the magic string ‘Local Variables:’; then it automatically discards them from the other lines of the list.

The usual reason for using a prefix and/or suffix is to embed the local variables list in a comment, so it won't confuse other programs that the file is intended as input for. The example above is for a language where comment lines start with ‘;; ’ and end with ‘**’; the local values for comment-start and comment-end customize the rest of Emacs for this unusual syntax. Don't use a prefix (or a suffix) if you don't need one.

If you write a multi-line string value, you should put the prefix and suffix on each line, even lines that start or end within the string. They will be stripped off for processing the list. If you want to split a long string across multiple lines of the file, you can use backslash-newline, which is ignored in Lisp string constants. Here's an example of doing this:

# Local Variables:
# compile-command: "cc foo.c -Dfoo=bar -Dhack=whatever \
#   -Dmumble=blaah"
# End:

Some “variable names” have special meanings in a local variables list. Specifying the “variable” mode really sets the major mode, while any value specified for the “variable” eval is simply evaluated as an expression (its value is ignored). A value for coding specifies the coding system for character code conversion of this file, and a value of t for unibyte says to visit the file in a unibyte buffer. These four “variables” are not really variables; setting them in any other context has no special meaning.

If mode is used to set a major mode, it should be the first “variable” in the list. Otherwise, the entries that precede it will usually be ignored, since most modes kill all local variables as part of their initialization.

You can use the mode “variable” to set minor modes as well as the major modes; in fact, you can use it more than once, first to set the major mode and then to set minor modes which are specific to particular buffers. But most minor modes should not be specified in the file at all, because they represent user preferences.

For example, you may be tempted to try to turn on Auto Fill mode with a local variable list. That is a mistake. The choice of Auto Fill mode or not is a matter of individual taste, not a matter of the contents of particular files. If you want to use Auto Fill, set up major mode hooks with your ‘.emacs’ file to turn it on (when appropriate) for you alone (see section The Init File, ‘~/.emacs). Don't use a local variable list to impose your taste on everyone.

The start of the local variables list must be no more than 3000 characters from the end of the file, and must be in the last page if the file is divided into pages. Otherwise, Emacs will not notice it is there. The purpose of this rule is so that a stray ‘Local Variables:’ not in the last page does not confuse Emacs, and so that visiting a long file that is all one page and has no local variables list need not take the time to search the whole file.

Use the command normal-mode to reset the local variables and major mode of a buffer according to the file name and contents, including the local variables list if any. See section How Major Modes are Chosen.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]
© 2000-2017
Individual documents may contain additional copyright information.