You are on page 1of 8

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.

org/TR/x
html1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>filetype.txt</title>
<style type="text/css">
.enscript-comment { font-style: italic; color: rgb(178,34,34); }
.enscript-function-name { font-weight: bold; color: rgb(0,0,255); }
.enscript-variable-name { font-weight: bold; color: rgb(184,134,11); }
.enscript-keyword { font-weight: bold; color: rgb(160,32,240); }
.enscript-reference { font-weight: bold; color: rgb(95,158,160); }
.enscript-string { font-weight: bold; color: rgb(188,143,143); }
.enscript-builtin { font-weight: bold; color: rgb(218,112,214); }
.enscript-type { font-weight: bold; color: rgb(34,139,34); }
.enscript-highlight { text-decoration: underline; color: 0; }
</style>
</head>
<body id="top">
<h1 style="margin:8px;" id="f1">filetype.txt&nbsp;&nbsp;&nbsp;<span style="fontweight: normal; font-size: 0.5em;">[<a href="http://opensource.apple.com//source
/vim/vim-6/vim/runtime/doc/filetype.txt?txt">plain text</a>]</span></h1>
<hr>
<div></div>
<pre>*filetype.txt* For Vim version 6.1. Last change: 2002 Mar 05
VIM REFERENCE MANUAL
Filetypes
1. Filetypes
2. Filetype plugin
3. Docs for the default filetype plugins.

by Bram Moolenaar
*filetype* *file-type*
|filetypes|
|filetype-plugin|
|ftplugin-docs|

Also see |autocmd.txt|.


{Vi does not have any of these commands}
==============================================================================
1. Filetypes
*filetypes* *file-types*
Vim can detect the type of file that is edited. This is done by checking the
file name and sometimes by inspecting the contents of the file for specific
text.
*:filetype* *:filet*
To enable file type detection, use this command in your vimrc: &gt;
:filetype on
Each time a new or existing file is edited, Vim will try to recognize the type
of the file and set the 'filetype' option. This will trigger the FileType
event, which can be used to set the syntax highlighting, set options, etc.
NOTE: Filetypes and 'compatible' don't work together well, since being Vi
compatible means options are global. Resetting 'compatible' is recommended,
if you didn't do that already.
Detail: The ":filetype on" command will load one of these files:
Amiga
$VIMRUNTIME/filetype.vim
Mac
$VIMRUNTIME:filetype.vim

MS-DOS
$VIMRUNTIME\filetype.vim
RiscOS
Vim:Filetype
Unix
$VIMRUNTIME/filetype.vim
VMS
$VIMRUNTIME/filetype.vim
This file is a Vim script that defines autocommands for the
BufNewFile and BufRead events. If the file type is not found by the
name, the file $VIMRUNTIME/scripts.vim is used to detect it from the
contents of the file.
To add your own file types, see |new-filetype| below.
If the file type is not detected automatically, or it finds the wrong type,
you can either set the 'filetype' option manually, or add a modeline to your
file. Example, for in an IDL file use the command: &gt;
:set filetype=idl
or add this |modeline| to the file: &gt;
/* vim: set filetype=idl : */
&lt;
*:filetype-plugin-on*
You can enable loading the plugin files for specific file types with: &gt;
:filetype plugin on
If filetype detection was not switched on yet, it will be as well.
This actually loads the file "ftplugin.vim" in 'runtimepath'.
The result is that when a file is edited its plugin file is loaded (if there
is one for the detected filetype). |filetype-plugin|
*:filetype-plugin-off*
You can disable it again with: &gt;
:filetype plugin off
The filetype detection is not switched off then. But if you do switch off
filetype detection, the plugins will not be loaded either.
This actually loads the file "ftplugof.vim" in 'runtimepath'.
*:filetype-indent-on*
You can enable loading the indent file for specific file types with: &gt;
:filetype indent on
If filetype detection was not switched on yet, it will be as well.
This actually loads the file "indent.vim" in 'runtimepath'.
The result is that when a file is edited its indent file is loaded (if there
is one for the detected filetype). |indent-expression|
*:filetype-indent-off*
You can disable it again with: &gt;
:filetype indent off
The filetype detection is not switched off then. But if you do switch off
filetype detection, the indent files will not be loaded either.
This actually loads the file "indoff.vim" in 'runtimepath'.
*:filetype-off*
To disable file type detection, use this command: &gt;
:filetype off
This will keep the flags for "plugin" and "indent", but since no file types
are being detected, they won't work until the next ":filetype on".
Overview:
command
:filetype on
:filetype off

*:filetype-overview*
detection
on
off

plugin
unchanged
unchanged

indent ~
unchanged
unchanged

:filetype
:filetype
:filetype
:filetype
:filetype
:filetype

plugin
plugin
indent
indent
plugin
plugin

on
off
on
off
indent on
indent off

on
unchanged
on
unchanged
on
unchanged

on
off
unchanged
unchanged
on
off

unchanged
unchanged
on
off
on
off

To see the current status, type: &gt;


:filetype
The output looks something like this: &gt;
filetype detection:ON plugin:ON indent:OFF
The file types are also used for syntax highlighting. If the ":syntax on"
command is used, the file type detection is installed too. There is no need
to do ":filetype on" after ":syntax on".
To disable one of the file types, add a line in the your filetype file, see
|remove-filetype|.
*filetype-detect*
To detect the file type again: &gt;
:filetype detect
Use this if you started with an empty file and typed text that makes it
possible to detect the file type. For example, when you entered this in a
shell script: "#!/bin/csh".
When filetype detection was off, it will be enabled first, like the "on"
argument was used.
*filetype-overrule*
When the same extension is used for two filetypes, Vim tries to guess what
kind of file it is. This doesn't always work. A number of global variables
can be used to overrule the filetype used for certain extensions:
file name
*.asa
*.asp
*.asm
*.prg
*.pl
*.inc
*.w
*.i
*.p
*.sh

variable ~
g:filetype_asa
g:filetype_asp
g:asmsyntax
g:filetype_prg
g:filetype_pl
g:filetype_inc
g:filetype_w
g:filetype_i
g:filetype_p
g:bash_is_sh

|aspvbs-syntax| |aspperl-syntax|
|aspvbs-syntax| |aspperl-syntax|
|asm-syntax|

|cweb-syntax|
|progress-syntax|
|pascal-syntax|
|sh-syntax|

*new-filetype*
If a file type that you want to use is not detected yet, there are two ways to
add it. In any way, it's better not modify the $VIMRUNTIME/filetype.vim file.
It will be overwritten when installing a new version of Vim.
A. If your file type can be detected by the file name.
1. Create your user runtime directory. You would normally use the first
item of the 'runtimepath' option. Example for Unix: &gt;
:!mkdir ~/.vim
&lt;
2. Create a file that contains autocommands to detect the file type.
Example: &gt;
" my filetype file
if exists("did_load_filetypes")
finish

endif
augroup filetypedetect
au! BufRead,BufNewFile *.mine
setfiletype mine
au! BufRead,BufNewFile *.xyz
setfiletype drawing
augroup END
&lt;
Write this file as "filetype.vim" in your user runtime directory. For
example, for Unix: &gt;
:w ~/.vim/filetype.vim
&lt; 3. To use the new filetype detection you must restart Vim.
Your filetype.vim will be sourced before the default FileType autocommands
have been installed. Your autocommands will match first, and the
":setfiletype" command will make sure that no other autocommands will set
'filetype' after this.
*new-filetype-scripts*
B. If your filetype can only be detected by inspecting the contents of the
file.
1. Create your user runtime directory. You would normally use the first
item of the 'runtimepath' option. Example for Unix: &gt;
:!mkdir ~/.vim
&lt;
2. Create a vim script file for doing this. Example: &gt;
if did_filetype()
" filetype already set..
finish
" ..don't do these checks
endif
if getline(1) =~ '^#!.*\&lt;mine\&gt;'
setfiletype mine
elseif getline(1) =~? '\&lt;drawing\&gt;'
setfiletype drawing
endif
&lt;
See $VIMRUNTIME/scripts.vim for more examples.
Write this file as "scripts.vim" in your user runtime directory. For
example, for Unix: &gt;
:w ~/.vim/scripts.vim
&lt;
3. The detection will work right away, no need to restart Vim.
Your scripts.vim is loaded before the default checks for file types, which
means that your rules override the default rules in
$VIMRUNTIME/scripts.vim.
*remove-filetype*
If a file type is detected that is wrong for you, install a filetype.vim or
scripts.vim to catch it (see above). You can set 'filetype' to a non-existing
name to avoid that it will be set later anyway: &gt;
:set filetype=ignored
If you are setting up a system with many users, and you don't want each user
to add/remove the same filetypes, consider writing the filetype.vim and
scripts.vim files in a runtime directory that is used for everybody. Check
the 'runtimepath' for a directory to use. If there isn't one, set
'runtimepath' in the |system-vimrc|. Be careful to keep the default
directories!
*autocmd-osfiletypes*
On operating systems which support storing a file type with the file, you can
specify that an autocommand should only be executed if the file is of a

certain type.
The actual type checking depends on which platform you are running Vim
on; see your system's documentation for details.
To use osfiletype checking in an autocommand you should put a list of types to
match in angle brackets in place of a pattern, like this: &gt;
:au BufRead *.html,&lt;&amp;faf;HTML&gt; so $VIMRUNTIME/syntax/html.vim
This will match:
- Any file whose name ends in `.html'
- Any file whose type is `&amp;faf' or 'HTML', where the meaning of these types
depends on which version of Vim you are using.
Unknown types are considered NOT to match.
You can also specify a type and a pattern at the same time (in which case they
must both match): &gt;
:au BufRead &lt;&amp;fff&gt;diff*
This will match files of type `&amp;fff' whose names start with `diff'.
Note that osfiletype checking is skipped if Vim is compiled without the
|+osfiletype| feature.
*plugin-details*
The "plugin" directory can be in any of the directories in the 'runtimepath'
option. All of these directories will be searched for plugins and they are
all loaded. For example, if this command: &gt;
set runtimepath
produces this output: &gt;
runtimepath=/etc/vim,~/.vim,/usr/local/share/vim/vim60
then Vim will load all plugins in these directories: &gt;
/etc/vim/plugin/
~/.vim/plugin/
/usr/local/share/vim/vim60/plugin/
Note that the last one is the value of $VIMRUNTIME which has been expanded.
What if it looks like your plugin is not being loaded? You can find out what
happens when Vim starts up by using the |-V| argument: &gt;
vim -V1
You will see a lot of messages, in between them is a remark about loading the
plugins. It starts with: &gt;
Searching for "plugin/*.vim" in
There you can see where Vim looks for your plugin scripts.
==============================================================================
2. Filetype plugin
*filetype-plugins*
When loading filetype plugins has been enabled |:filetype-plugin-on|, options
will be set and mappings defined. These are all local to the buffer, they
will not be used for other files.

Defining mappings for a filetype may get in the way of the mappings you
define yourself. There are a few ways to avoid this:
1. Set the "maplocalleader" variable to the key sequence you want the mappings
to start with. Example: &gt;
:let maplocalleader = ","
&lt; All mappings will then start with a comma instead of the default, which
is a backslash. Also see |&lt;LocalLeader&gt;|.
2. Define your own mapping. Example: &gt;
:map ,p &lt;Plug&gt;MailQuote
&lt; You need to check the description of the plugin file below for the
functionality it offers and the string to map to.
You need to define your own mapping before the plugin is loaded (before
editing a file of that type). The plugin will then skip installing the
default mapping.
3. Disable defining mappings for a specific filetype by setting a variable,
which contains the name of the filetype. For the "mail" filetype this
would be: &gt;
:let no_mail_maps = 1
4. Disable defining mappings for all filetypes by setting a variable: &gt;
:let no_plugin_maps = 1
&lt;
*ftplugin-overrule*
If a global filetype plugin does not do exactly what you want, there are three
ways to change this:
1. Add a few settings.
You must create a new filetype plugin in a directory early in
'runtimepath'. For Unix, for example you could use this file: &gt;
vim ~/.vim/ftplugin/fortran.vim
&lt; You can set those settings and mappings that you would like to add. Note
that the global plugin will be loaded after this, it may overrule the
settings that you do here. If this is the case, you need to use one of the
following two methods.
2. Make a copy of the plugin and change it.
You must put the copy in a directory early in 'runtimepath'. For Unix, for
example, you could do this: &gt;
cp $VIMRUNTIME/ftplugin/fortran.vim ~/.vim/ftplugin/fortran.vim
&lt; Then you can edit the copied file to your liking. Since the b:did_ftplugi
n
variable will be set, the global plugin will not be loaded.
A disadvantage of this method is that when the distributed plugin gets
improved, you will have to copy and modify it again.
3. Overrule the settings after loading the global plugin.
You must create a new filetype plugin in a directory from the end of
'runtimepath'. For Unix, for example, you could use this file: &gt;
vim ~/.vim/after/ftplugin/fortran.vim
&lt; In this file you can change just those settings that you want to change.
==============================================================================
3. Docs for the default filetype plugins.
*ftplugin-docs*
FORTRAN

*fortran-plugin*

Options:
'expandtab'

is switched on to avoid tabs as required by the Fortran


standards unless the user has set fortran_have_tabs in .vimrc.
'textwidth'
is set to 72 for fixed source format as required by the
Fortran standards and to 80 for free source format.
'formatoptions' is set to break code and comment lines and to preserve long
lines. You can format comments with |gq|.
For further discussion of fortran_have_tabs and the method used for the
detection of source format see |fortran-syntax|.
MAIL

*mail-plugin*

Options:
'modeline'

is switched off to avoid the danger of trojan horses, and to


avoid that a Subject line with "Vim:" in it will cause an
error message.
'textwidth'
is set to 72. This is often recommended for e-mail.
'formatoptions' is set to break text lines and to repeat the comment leader
in new lines, so that a leading "&gt;" for quotes is repeated.
You can also format quoted text with |gq|.
Local mappings:
&lt;LocalLeader&gt;q or \\MailQuote
Quotes the text selected in Visual mode, or from the cursor position
to the end of the file in Normal mode. This means "&gt; " is inserted i
n
each line.
MAN

*man-plugin*

Displays a manual page in a nice way.


To start using the ":Man" command before any manual page was loaded, source
this script from your startup vimrc file: &gt;
runtime ftplugin/man.vim
Options:
'iskeyword'
Commands:
Man {name}

the '.' character is added to be able to use CTRL-] on the


manual page name.
Display the manual page for {name} in a window.

Global mapping:
&lt;Leader&gt;K Displays the manual page for the word under the cursor.
Local mappings:
CTRL-]
Jump to the manual page for the word under the cursor.
CTRL-T
Jump back to the previous manual page.
RPM SPEC

*spec-plugin*

Since the text for this plugin is rather long it has been put in a separate
file: |pi_spec.txt|.

vim:tw=78:ts=8:ft=help:norl:
</pre>
<hr>
</body></html>

You might also like