Vim Tips Wiki
m (Reverted edits by 113.98.195.81 (talk | block) to last version by JohnBeckett)
(Change <tt> to <code>, perhaps also minor tweak.)
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
This tip is a tutorial on the use of the file <tt>filetype.vim</tt> which is used to determine the "type" of a file. For example, while editing <tt>example.py</tt> the command {{tt|:set ft?}} should display <tt>filetype=python</tt> if {{tt|:filetype indent plugin on}} has been used. The file type determines whether any plugins for scripts, indenting rules, or syntax highlighting are loaded. See [[file type plugins]] for an overview.
+
This tip is a tutorial on the use of the file <code>filetype.vim</code> which is used to determine the "type" of a file. For example, while editing <code>example.py</code> the command {{tt|:set ft?}} should display <code>filetype=python</code> if {{tt|:filetype indent plugin on}} has been used. The file type determines whether any plugins for scripts, indenting rules, or syntax highlighting are loaded. See [[file type plugins]] for an overview.
   
 
==File structure==
 
==File structure==
Custom <tt>filetype.vim</tt> files should always have the following structure:
+
Custom <code>filetype.vim</code> files should always have the following structure:
 
<pre>
 
<pre>
 
if exists("did_load_filetypes")
 
if exists("did_load_filetypes")
Line 24: Line 24:
 
</pre>
 
</pre>
   
Files that are being used as fallbacks (in the <tt>after</tt> directories) should use a form such as
+
Files that are being used as fallbacks (in the <code>after</code> directories) should use a form such as
 
<pre>
 
<pre>
 
if exists("did_load_filetypes_userafter")
 
if exists("did_load_filetypes_userafter")
Line 35: Line 35:
 
</pre>
 
</pre>
   
because <tt>did_load_filetypes</tt> will be set before the <tt>after</tt> files are used. Using the suffixes <tt>_userafter</tt> and <tt>_systemafter</tt> as appropriate will prevent one from interfering with the other.
+
because <code>did_load_filetypes</code> will be set before the <code>after</code> files are used. Using the suffixes <code>_userafter</code> and <code>_systemafter</code> as appropriate will prevent one from interfering with the other.
   
 
==File contents==
 
==File contents==
Between <tt>augroup filetypedetect</tt> and <tt>augroup END</tt> there will be one or more <tt>autocmd</tt> statements that may assign a filetype when the <tt>BufNewFile</tt> and <tt>BufRead</tt> events occur (that is, when a new file is created, or when an existing file is read).
+
Between <code>augroup filetypedetect</code> and <code>augroup END</code> there will be one or more <code>autocmd</code> statements that may assign a filetype when the <code>BufNewFile</code> and <code>BufRead</code> events occur (that is, when a new file is created, or when an existing file is read).
   
 
Following are some examples to illustrate common scenarios.
 
Following are some examples to illustrate common scenarios.
   
Assign the <tt>dosbatch</tt> filetype to any file with name ending in <tt>.bat</tt> or <tt>.sys</tt>:
+
Assign the <code>dosbatch</code> filetype to any file with name ending in <code>.bat</code> or <code>.sys</code>:
 
<pre>
 
<pre>
 
au! BufNewFile,BufRead *.bat,*.sys setf dosbatch
 
au! BufNewFile,BufRead *.bat,*.sys setf dosbatch
 
</pre>
 
</pre>
   
If the first line of a <tt>.cmd</tt> file begins with <tt>/*</tt> then assign the filetype <tt>rexx</tt>, otherwise assign the filetype <tt>dosbatch</tt>:
+
If the first line of a <code>.cmd</code> file begins with <code>/*</code> then assign the filetype <code>rexx</code>, otherwise assign the filetype <code>dosbatch</code>:
 
<pre>
 
<pre>
 
au! BufNewFile,BufRead *.cmd if getline(1) =~ '^/\*' | setf rexx | else | setf dosbatch | endif
 
au! BufNewFile,BufRead *.cmd if getline(1) =~ '^/\*' | setf rexx | else | setf dosbatch | endif
 
</pre>
 
</pre>
   
If the first line of a <tt>.bat</tt> file contains "<tt>--*-Perl-*--</tt>" then assign the filetype <tt>perl</tt>, otherwise do nothing and continue processing rules (see [[Editing ActiveState Perl batch files|tip]]):
+
If the first line of a <code>.bat</code> file contains "<code>--*-Perl-*--</code>" then assign the filetype <code>perl</code>, otherwise do nothing and continue processing rules (see [[Editing ActiveState Perl batch files|tip]]):
 
<pre>
 
<pre>
 
au! BufRead,BufNewFile *.bat if getline(1) =~ '--\*-Perl-\*--' | setf perl | endif
 
au! BufRead,BufNewFile *.bat if getline(1) =~ '--\*-Perl-\*--' | setf perl | endif
 
</pre>
 
</pre>
   
When reading a <tt>.btm</tt> file, if the variable <tt>g:dosbatch_syntax_for_btm</tt> is defined and non-zero then assign the <tt>dosbatch</tt> filetype, otherwise assign the <tt>btm</tt> filetype.
+
When reading a <code>.btm</code> file, if the variable <code>g:dosbatch_syntax_for_btm</code> is defined and non-zero then assign the <code>dosbatch</code> filetype, otherwise assign the <code>btm</code> filetype.
 
<pre>
 
<pre>
 
au! BufNewFile,BufRead *.btm call s:FTbtm()
 
au! BufNewFile,BufRead *.btm call s:FTbtm()
Line 70: Line 70:
   
 
==File locations==
 
==File locations==
The file <tt>filetype.vim</tt> is read from directories in the runtime path. The first match which executes <tt>:setf</tt> will set the filetype for the file Vim is creating or reading. If no rules execute <tt>:setf</tt> then additional <tt>filetype.vim</tt> files will be read.
+
The file <code>filetype.vim</code> is read from directories in the runtime path. The first match which executes <code>:setf</code> will set the filetype for the file Vim is creating or reading. If no rules execute <code>:setf</code> then additional <code>filetype.vim</code> files will be read.
   
 
The file locations (in the order that the files are processed) are:
 
The file locations (in the order that the files are processed) are:
 
;User-specific primary definitions
 
;User-specific primary definitions
 
:Rules for the current user that should be attempted before any other rule.
 
:Rules for the current user that should be attempted before any other rule.
:<tt>$HOME/.vim/filetype.vim</tt> (Unix based systems)
+
:<code>$HOME/.vim/filetype.vim</code> (Unix based systems)
:<tt>$HOME\vimfiles\filetype.vim</tt> (Windows systems)
+
:<code>$HOME\vimfiles\filetype.vim</code> (Windows systems)
 
;System primary definitions
 
;System primary definitions
 
:Rules for all users on a system (usually requires administrative rights to edit).
 
:Rules for all users on a system (usually requires administrative rights to edit).
:<tt>$VIM/vimfiles/filetype.vim</tt> (all systems)
+
:<code>$VIM/vimfiles/filetype.vim</code> (all systems)
 
;Vim default ruleset
 
;Vim default ruleset
 
:Rules that are part of the Vim install (do ''not'' edit this file).
 
:Rules that are part of the Vim install (do ''not'' edit this file).
:<tt>$VIMRUNTIME/filetype.vim</tt> (all systems)
+
:<code>$VIMRUNTIME/filetype.vim</code> (all systems)
 
;System fallback definitions
 
;System fallback definitions
 
:Rules for all users if no other rule has matched so far. Useful for a rule that you would want to stop using if a future version of Vim defined a better match.
 
:Rules for all users if no other rule has matched so far. Useful for a rule that you would want to stop using if a future version of Vim defined a better match.
:<tt>$VIM/vimfiles/after/filetype.vim</tt> (all systems)
+
:<code>$VIM/vimfiles/after/filetype.vim</code> (all systems)
 
;User-specific fallback definitions
 
;User-specific fallback definitions
 
:Rules for the current user if no other rule has matched so far.
 
:Rules for the current user if no other rule has matched so far.
:<tt>$HOME/.vim/after/filetype.vim</tt> (Unix based systems)
+
:<code>$HOME/.vim/after/filetype.vim</code> (Unix based systems)
:<tt>$HOME\vimfiles\after\filetype.vim</tt> (Windows systems)
+
:<code>$HOME\vimfiles\after\filetype.vim</code> (Windows systems)
   
 
In Vim, use commands like the following to check these locations:
 
In Vim, use commands like the following to check these locations:
Line 98: Line 98:
 
</pre>
 
</pre>
   
On Windows systems, <tt>$HOME</tt> is set from the environment variable <tt>HOME</tt>, if defined; or is set by joining variables <tt>HOMEDRIVE</tt> and <tt>HOMEPATH</tt>, if they are defined. At command prompt (not in Vim), enter {{tt|set H}} to display the environment variables that begin with '<tt>H</tt>'.
+
On Windows systems, <code>$HOME</code> is set from the environment variable <code>HOME</code>, if defined; or is set by joining variables <code>HOMEDRIVE</code> and <code>HOMEPATH</code>, if they are defined. At command prompt (not in Vim), enter {{tt|set H}} to display the environment variables that begin with '<code>H</code>'.
   
 
==Example==
 
==Example==
When using our [[Working with CSV files|CSV tip]], it is necessary to specify that <tt>*.csv</tt> files have the <tt>csv</tt> file type. To do this, edit the following file:
+
When using our [[Working with CSV files|CSV tip]], it is necessary to specify that <code>*.csv</code> files have the <code>csv</code> file type. To do this, edit the following file:
*<tt>$HOME/.vim/filetype.vim</tt> (Unix based systems)
+
*<code>$HOME/.vim/filetype.vim</code> (Unix based systems)
*<tt>$HOME/vimfiles/filetype.vim</tt> (Windows systems – can use forward slash or backslash in Vim)
+
*<code>$HOME/vimfiles/filetype.vim</code> (Windows systems – can use forward slash or backslash in Vim)
   
Check that <tt>$HOME</tt> has been defined, and that the <tt>$HOME/.vim</tt> directory (Unix) or <tt>$HOME/vimfiles</tt> directory (Windows) exists, and you may need to create the <tt>filetype.vim</tt> file in that directory.
+
Check that <code>$HOME</code> has been defined, and that the <code>$HOME/.vim</code> directory (Unix) or <code>$HOME/vimfiles</code> directory (Windows) exists, and you may need to create the <code>filetype.vim</code> file in that directory.
   
Contents of user <tt>filetype.vim</tt> (which may include other rules):
+
Contents of user <code>filetype.vim</code> (which may include other rules):
 
<pre>
 
<pre>
 
if exists("did_load_filetypes")
 
if exists("did_load_filetypes")
Line 117: Line 117:
 
</pre>
 
</pre>
   
As well as configuring <tt>filetype.vim</tt>, you may need the following commands (which would normally be in your [[vimrc]]) to enable all the features available for a particular file type.
+
As well as configuring <code>filetype.vim</code>, you may need the following commands (which would normally be in your [[vimrc]]) to enable all the features available for a particular file type.
 
<pre>
 
<pre>
 
:set nocompatible
 
:set nocompatible

Revision as of 07:56, 11 July 2012

Tip 1606 Printable Monobook Previous Next

created 2008 · complexity basic · author JeremyBarton · version 7.0


This tip is a tutorial on the use of the file filetype.vim which is used to determine the "type" of a file. For example, while editing example.py the command :set ft? should display filetype=python if :filetype indent plugin on has been used. The file type determines whether any plugins for scripts, indenting rules, or syntax highlighting are loaded. See file type plugins for an overview.

File structure

Custom filetype.vim files should always have the following structure:

if exists("did_load_filetypes")
  finish
endif
augroup filetypedetect
  " au! commands to set the filetype go here
augroup END

Files that are being used as fallbacks (in the after directories) should use a form such as

if exists("did_load_filetypes_userafter")
  finish
endif
let did_load_filetypes_userafter = 1
augroup filetypedetect
  " au! commands to set the filetype go here
augroup END

because did_load_filetypes will be set before the after files are used. Using the suffixes _userafter and _systemafter as appropriate will prevent one from interfering with the other.

File contents

Between augroup filetypedetect and augroup END there will be one or more autocmd statements that may assign a filetype when the BufNewFile and BufRead events occur (that is, when a new file is created, or when an existing file is read).

Following are some examples to illustrate common scenarios.

Assign the dosbatch filetype to any file with name ending in .bat or .sys:

au! BufNewFile,BufRead *.bat,*.sys setf dosbatch

If the first line of a .cmd file begins with /* then assign the filetype rexx, otherwise assign the filetype dosbatch:

au! BufNewFile,BufRead *.cmd if getline(1) =~ '^/\*' | setf rexx | else | setf dosbatch | endif

If the first line of a .bat file contains "--*-Perl-*--" then assign the filetype perl, otherwise do nothing and continue processing rules (see tip):

au! BufRead,BufNewFile *.bat if getline(1) =~ '--\*-Perl-\*--' | setf perl | endif

When reading a .btm file, if the variable g:dosbatch_syntax_for_btm is defined and non-zero then assign the dosbatch filetype, otherwise assign the btm filetype.

au! BufNewFile,BufRead *.btm call s:FTbtm()
function! s:FTbtm()
  if exists("g:dosbatch_syntax_for_btm") && g:dosbatch_syntax_for_btm
    setf dosbatch
  else
    setf btm
  endif
endfunction

File locations

The file filetype.vim is read from directories in the runtime path. The first match which executes :setf will set the filetype for the file Vim is creating or reading. If no rules execute :setf then additional filetype.vim files will be read.

The file locations (in the order that the files are processed) are:

User-specific primary definitions
Rules for the current user that should be attempted before any other rule.
$HOME/.vim/filetype.vim (Unix based systems)
$HOME\vimfiles\filetype.vim (Windows systems)
System primary definitions
Rules for all users on a system (usually requires administrative rights to edit).
$VIM/vimfiles/filetype.vim (all systems)
Vim default ruleset
Rules that are part of the Vim install (do not edit this file).
$VIMRUNTIME/filetype.vim (all systems)
System fallback definitions
Rules for all users if no other rule has matched so far. Useful for a rule that you would want to stop using if a future version of Vim defined a better match.
$VIM/vimfiles/after/filetype.vim (all systems)
User-specific fallback definitions
Rules for the current user if no other rule has matched so far.
$HOME/.vim/after/filetype.vim (Unix based systems)
$HOME\vimfiles\after\filetype.vim (Windows systems)

In Vim, use commands like the following to check these locations:

:echo $HOME
:echo $VIM
:echo $VIMRUNTIME

On Windows systems, $HOME is set from the environment variable HOME, if defined; or is set by joining variables HOMEDRIVE and HOMEPATH, if they are defined. At command prompt (not in Vim), enter set H to display the environment variables that begin with 'H'.

Example

When using our CSV tip, it is necessary to specify that *.csv files have the csv file type. To do this, edit the following file:

  • $HOME/.vim/filetype.vim (Unix based systems)
  • $HOME/vimfiles/filetype.vim (Windows systems – can use forward slash or backslash in Vim)

Check that $HOME has been defined, and that the $HOME/.vim directory (Unix) or $HOME/vimfiles directory (Windows) exists, and you may need to create the filetype.vim file in that directory.

Contents of user filetype.vim (which may include other rules):

if exists("did_load_filetypes")
  finish
endif
augroup filetypedetect
  au! BufNewFile,BufRead *.csv setf csv
augroup END

As well as configuring filetype.vim, you may need the following commands (which would normally be in your vimrc) to enable all the features available for a particular file type.

:set nocompatible
:filetype indent plugin on
:syntax on

References

Comments