Impostare determinate opzioni di vim all'apertura di un file

From RVM Wiki
Revision as of 16:07, 7 December 2007 by Gabriele.vivinetto (talk | contribs)
Jump to navigation Jump to search

Se in un file, verso l'inzio o la fine, si inseriscono dei commenti che iniziano per:

# vim: opzione

Vim attiverà queste opzioni.

  • Esempio: per impostare la lunghezza VISUALIZZATA della tabulazione a 12 caratteri, inserire alla fine del file:
# vim: sw=3 ts=12
  • Si veda l'help
help auto-settings

Terza opzione, modeline:

3. If you start editing a new file, and the 'modeline' option is on, a
   number of lines at the beginning and end of the file are checked for
   modelines.  This is explained here.

					*modeline* *vim:* *vi:* *ex:* *E520*
There are two forms of modelines.  The first form:
	[text]{white}{vi:|vim:|ex:}[white]{options}

[text]		any text or empty
{white}		at least one blank character (<Space> or <Tab>)
{vi:|vim:|ex:}	the string "vi:", "vim:" or "ex:"
[white]		optional white space
{options}	a list of option settings, separated with white space or ':',
		where each part between ':' is the argument for a ":set"
		command

Example:
   vi:noai:sw=3 ts=6 ~

The second form (this is compatible with some versions of Vi):

	[text]{white}{vi:|vim:|ex:}[white]se[t] {options}:[text]

[text]		any text or empty
{white}		at least one blank character (<Space> or <Tab>)
{vi:|vim:|ex:}	the string "vi:", "vim:" or "ex:"
[white]		optional white space
se[t]		the string "set " or "se " (note the space)
{options}	a list of options, separated with white space, which is the
		argument for a ":set" command
:		a colon
[text]		any text or empty

Example:
   /* vim: set ai tw=75: */ ~

The white space before {vi:|vim:|ex:} is required.  This minimizes the chance
that a normal word like "lex:" is caught.  There is one exception: "vi:" and
"vim:" can also be at the start of the line (for compatibility with version
3.0).  Using "ex:" at the start of the line will be ignored (this could be
short for "example:").

							*modeline-local*
The options are set like with ":setlocal": The new value only applies to the
buffer and window that contain the file.  Although it's possible to set global
options from a modeline, this is unusual.  If you have two windows open and
the files in it set the same global option to a different value, the result
depends on which one was opened last.

When editing a file that was already loaded, only the window-local options
from the modeline are used.  Thus if you manually changed a buffer-local
option after opening the file, it won't be changed if you edit the same buffer
in another window.  But window-local options will be set.

							*modeline-version*
If the modeline is only to be used for some versions of Vim, the version
number can be specified where "vim:" is used:
	vim{vers}:	version {vers} or later
	vim<{vers}:	version before {vers}
	vim={vers}:	version {vers}
	vim>{vers}:	version after {vers}
{vers} is 600 for Vim 6.0 (hundred times the major version plus minor).
For example, to use a modeline only for Vim 6.0 and later:
	/* vim600: set foldmethod=marker: */ ~
To use a modeline for Vim before version 5.7:
	/* vim<570: set sw=4: */ ~
There can be no blanks between "vim" and the ":".


The number of lines that are checked can be set with the 'modelines' option.
If 'modeline' is off or 'modelines' is 0 no lines are checked.

Note that for the first form all of the rest of the line is used, thus a line
like:
   /* vi:ts=4: */ ~
will give an error message for the trailing "*/".  This line is OK:
   /* vi:set ts=4: */ ~

If an error is detected the rest of the line is skipped.

If you want to include a ':' in a set command precede it with a '\'.  The
backslash in front of the ':' will be removed.  Example:
   /* vi:set dir=c\:\tmp: */ ~
This sets the 'dir' option to "c:\tmp".  Only a single backslash before the
':' is removed.  Thus to include "\:" you have to specify "\\:".

No other commands than "set" are supported, for security reasons (somebody
might create a Trojan horse text file with modelines).

Hint: If you would like to do something else than setting an option, you could
define an autocommand that checks the file for a specific string.  For
example: >
	au BufReadPost * if getline(1) =~ "VAR" | call SetVar() | endif
And define a function SetVar() that does something with the line containing
"VAR".