JeremyBarton (talk | contribs) m (→Match conditionally on a file extension (simple): Missing a / in /tt) |
JohnBeckett (talk | contribs) (Standardise with TipNew) |
||
Line 1: | Line 1: | ||
+ | {{TipNew |
||
− | [[Category:Filetype]] |
||
+ | |id=0 |
||
+ | |previous= |
||
+ | |next=1 |
||
+ | |created=September 30, 2008 |
||
+ | |complexity=basic |
||
+ | |author=JeremyBarton |
||
+ | |version=7.0 |
||
+ | |subpage=/200809 |
||
+ | |category1=Filetype |
||
+ | |category2= |
||
+ | }} |
||
Filetype.vim is the name of the file that is used by the filetype plugin ([[:Category:Filetype]]). |
Filetype.vim is the name of the file that is used by the filetype plugin ([[:Category:Filetype]]). |
||
Line 25: | Line 36: | ||
</pre> |
</pre> |
||
− | because <code>did_load_filetypes</code> will be set before execution gets to the <tt>after</tt> files. |
+ | because <code>did_load_filetypes</code> will be set before execution gets to the <tt>after</tt> files. Using the suffixes <tt>_userafter</tt> and <tt>_machineafter</tt> as appropriate will prevent one from interfering with the other. |
==File Contents== |
==File Contents== |
||
− | Between <code>augroup filetypedetect</code> and <code>augroup END</code> you will have one or more <code>au[tocmd]</code> directives, registered against the <code>BufNewFile</code> and <code>BufRead</code> events, to assign the filetype. |
+ | Between <code>augroup filetypedetect</code> and <code>augroup END</code> you will have one or more <code>au[tocmd]</code> directives, registered against the <code>BufNewFile</code> and <code>BufRead</code> events, to assign the filetype. |
===Common rule formats=== |
===Common rule formats=== |
||
Line 45: | Line 56: | ||
====Match conditionally on a file extension for override (simple)==== |
====Match conditionally on a file extension for override (simple)==== |
||
<pre>au! BufRead,BufNewFile *.bat if getline(1) =~ '--\*-Perl-\*--' | setf perl | endif</pre> |
<pre>au! BufRead,BufNewFile *.bat if getline(1) =~ '--\*-Perl-\*--' | setf perl | endif</pre> |
||
− | If the first line of a <tt>.bat</tt> file begins with <tt>--*-Perl-*--</tt> then assign the filetype <tt>perl</tt>, otherwise do nothing and continue processing rules. |
+ | If the first line of a <tt>.bat</tt> file begins with <tt>--*-Perl-*--</tt> then assign the filetype <tt>perl</tt>, otherwise do nothing and continue processing rules. (Referenced from [[Editing ActiveState Perl batch files]].) |
====Match conditionally on a file extension (complex)==== |
====Match conditionally on a file extension (complex)==== |
||
Line 59: | Line 70: | ||
endfunc |
endfunc |
||
</pre> |
</pre> |
||
+ | |||
When reading a <tt>.btm</tt> file, if <code>g:dosbatch_syntax_for_btm</code> is defined and non-zero then assign the <tt>dosbatch</tt> filetype, otherwise assign the <tt>btm</tt> filetype. |
When reading a <tt>.btm</tt> file, if <code>g:dosbatch_syntax_for_btm</code> is defined and non-zero then assign the <tt>dosbatch</tt> filetype, otherwise assign the <tt>btm</tt> filetype. |
||
==File Locations== |
==File Locations== |
||
− | The plugin will load filetype.vim file(s) from the runtime path. |
+ | The plugin will load filetype.vim file(s) from the runtime path. The first match which calls <code>setf</code> will be the resulting filetype for the affected file. If no rules match, or the rule fails to eventually call <code>setf</code> then rules from successive files will be attempted. |
* User-specific primary definitions |
* User-specific primary definitions |
||
− | ** Contains rules for the current user that should be attempted before any other rule. |
+ | ** Contains rules for the current user that should be attempted before any other rule. If your rule is conditional and does not call <code>setf</code> then later rules will still be attempted. |
*** Unix: <tt>$HOME/.vim/filetype.vim</tt> |
*** Unix: <tt>$HOME/.vim/filetype.vim</tt> |
||
*** Win32: <tt>%USERPROFILE%\vimfiles\filetype.vim</tt> |
*** Win32: <tt>%USERPROFILE%\vimfiles\filetype.vim</tt> |
||
* Machine-local primary definitions |
* Machine-local primary definitions |
||
− | ** Contains rules that should apply to all users on a machine. |
+ | ** Contains rules that should apply to all users on a machine. This file usually requires some sort of administrative rights to edit. |
*** <tt>$VIM/vimfiles/filetype.vim</tt> |
*** <tt>$VIM/vimfiles/filetype.vim</tt> |
||
* Vim default ruleset |
* Vim default ruleset |
||
− | ** Contains rules that are part of the Vim install. |
+ | ** Contains rules that are part of the Vim install. DO NOT edit this file. |
*** <tt>$VIMRUNTIME/filetype.vim</tt> |
*** <tt>$VIMRUNTIME/filetype.vim</tt> |
||
* Machine-local fallback definitions |
* Machine-local fallback definitions |
||
− | ** Contains rules that should apply to all users on a machine only if no other rule has matched so far. |
+ | ** Contains rules that should apply to all users on a machine only if no other rule has matched so far. This is useful when you have 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> |
*** <tt>$VIM/vimfiles/after/filetype.vim</tt> |
||
* User-specific fallback definitions |
* User-specific fallback definitions |
||
Line 83: | Line 95: | ||
==See also== |
==See also== |
||
− | * |
+ | *{{help|filetype}} |
− | * |
+ | *{{help|new-filetype}} |
− | * |
+ | *{{help|ft}} |
− | * |
+ | *{{help|setf}} |
− | * |
+ | *{{help|augroup}} |
− | * |
+ | *{{help|autocmd}} |
− | * |
+ | *{{help|BufNewFile}} |
− | * |
+ | *{{help|BufRead}} |
− | * |
+ | *{{help|getline}} |
− | * |
+ | *{{help|user-functions}} |
− | * |
+ | *{{help|runtimepath}} |
− | * |
+ | *{{help|$VIM}} |
− | * |
+ | *{{help|$VIMRUNTIME}} |
==Comments== |
==Comments== |
||
Line 101: | Line 113: | ||
Also, on Windows, %HOMEDRIVE%%HOMEPATH% is used before %USERPROFILE% for the location of $HOME. I'm not sure how much of the stuff in {{help|todo.txt}} regarding the setting of $HOME in Windows is implemented, but I do know that my Windows installation picks up %HOMEDRIVE%%HOMEPATH% and not %USERPROFILE%. |
Also, on Windows, %HOMEDRIVE%%HOMEPATH% is used before %USERPROFILE% for the location of $HOME. I'm not sure how much of the stuff in {{help|todo.txt}} regarding the setting of $HOME in Windows is implemented, but I do know that my Windows installation picks up %HOMEDRIVE%%HOMEPATH% and not %USERPROFILE%. |
||
− | :Looks like you're right. |
+ | :Looks like you're right. Vim71 uses %HOMEDRIVE%%HOMEPATH%, which is arguably broken (as it has been, neither runas or (presumably) roaming profiles work right), but current implementations don't use %USERPROFILE% at all. I guess it can just be $HOME as a wiki link and in that page a Win32 section explains it. Not sure if I'm more disappointed in Windows or Vim on this one. --[[User:JeremyBarton|JeremyBarton]] 16:49, 30 September 2008 (UTC) |
+ | |||
+ | ---- |
||
+ | Problems, problems... I'm processing the proposed new tips for September. Apart from redirects, every page in our Main namespace is a tip, and has one of these templates: [[Template:TipImported|TipImported]], [[Template:TipNew|TipNew]], [[Template:TipProposed|TipProposed]]. I'm not going to put TipProposed on this (has an ugly box). Probably there should be ''no'' template and this should just be a normal Wikipedia-style page. However, I'm not ready to make decisions like that because I'm still in frantic "must clean up the old tips" mode. Also, it's ''really'' handy having simple tip numbers to refer to pages while doing lots of maintenance work. So, I've put TipNew with a dummy tip number and will think about it later. Let's delete my comment in a week or so, when everyone has had a chance to see it. --[[User:JohnBeckett|JohnBeckett]] 00:40, 1 October 2008 (UTC) |
||
+ | |||
+ | ---- |
Revision as of 00:40, 1 October 2008
created September 30, 2008 · complexity basic · author JeremyBarton · version 7.0
Filetype.vim is the name of the file that is used by the filetype plugin (Category:Filetype).
File Structure
Custom filetype.vim files should always have the following structure:
if exists("did_load_filetypes") finish endif augroup filetypedetect " local filetype changes 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 " local filetype changes go here augroup END
because did_load_filetypes
will be set before execution gets to the after files. Using the suffixes _userafter and _machineafter as appropriate will prevent one from interfering with the other.
File Contents
Between augroup filetypedetect
and augroup END
you will have one or more au[tocmd]
directives, registered against the BufNewFile
and BufRead
events, to assign the filetype.
Common rule formats
The following examples are primarily taken from the Vim default ruleset on Vim 7.1.
Always match a specified file extension
au BufNewFile,BufRead *.bat,*.sys setf dosbatch
Always assigns the dosbatch filetype to any file ending in .bat or .sys.
Match conditionally on a file extension (simple)
au BufNewFile,BufRead *.cmd if getline(1) =~ '^/\*' | setf rexx | else | setf dosbatch | endif
If the first line of a .cmd file begins with /* then assign the filetype rexx, otherwise assign the filetype dosbatch.
Match conditionally on a file extension for override (simple)
au! BufRead,BufNewFile *.bat if getline(1) =~ '--\*-Perl-\*--' | setf perl | endif
If the first line of a .bat file begins with --*-Perl-*-- then assign the filetype perl, otherwise do nothing and continue processing rules. (Referenced from Editing ActiveState Perl batch files.)
Match conditionally on a file extension (complex)
au BufNewFile,BufRead *.btm call s:FTbtm() func! s:FTbtm() if exists("g:dosbatch_syntax_for_btm") && g:dosbatch_syntax_for_btm setf dosbatch else setf btm endif endfunc
When reading a .btm file, if g:dosbatch_syntax_for_btm
is defined and non-zero then assign the dosbatch filetype, otherwise assign the btm filetype.
File Locations
The plugin will load filetype.vim file(s) from the runtime path. The first match which calls setf
will be the resulting filetype for the affected file. If no rules match, or the rule fails to eventually call setf
then rules from successive files will be attempted.
- User-specific primary definitions
- Contains rules for the current user that should be attempted before any other rule. If your rule is conditional and does not call
setf
then later rules will still be attempted.- Unix: $HOME/.vim/filetype.vim
- Win32: %USERPROFILE%\vimfiles\filetype.vim
- Contains rules for the current user that should be attempted before any other rule. If your rule is conditional and does not call
- Machine-local primary definitions
- Contains rules that should apply to all users on a machine. This file usually requires some sort of administrative rights to edit.
- $VIM/vimfiles/filetype.vim
- Contains rules that should apply to all users on a machine. This file usually requires some sort of administrative rights to edit.
- Vim default ruleset
- Contains rules that are part of the Vim install. DO NOT edit this file.
- $VIMRUNTIME/filetype.vim
- Contains rules that are part of the Vim install. DO NOT edit this file.
- Machine-local fallback definitions
- Contains rules that should apply to all users on a machine only if no other rule has matched so far. This is useful when you have a rule that you would want to stop using if a future version of Vim defined a better match.
- $VIM/vimfiles/after/filetype.vim
- Contains rules that should apply to all users on a machine only if no other rule has matched so far. This is useful when you have a rule that you would want to stop using if a future version of Vim defined a better match.
- User-specific fallback definitions
- Contains rules for the current user that should only be attempted if no other rule has matched so far.
- Unix: $HOME/.vim/after/filetype.vim
- Win32: %USERPROFILE%\vimfiles\after\filetype.vim
- Contains rules for the current user that should only be attempted if no other rule has matched so far.
See also
- :help filetype
- :help new-filetype
- :help ft
- :help setf
- :help augroup
- :help autocmd
- :help BufNewFile
- :help BufRead
- :help getline
- :help user-functions
- :help runtimepath
- :help $VIM
- :help $VIMRUNTIME
Comments
Maybe we should mention the "ftdetect" directory. :help ftdetect
Also, on Windows, %HOMEDRIVE%%HOMEPATH% is used before %USERPROFILE% for the location of $HOME. I'm not sure how much of the stuff in :help todo.txt regarding the setting of $HOME in Windows is implemented, but I do know that my Windows installation picks up %HOMEDRIVE%%HOMEPATH% and not %USERPROFILE%.
- Looks like you're right. Vim71 uses %HOMEDRIVE%%HOMEPATH%, which is arguably broken (as it has been, neither runas or (presumably) roaming profiles work right), but current implementations don't use %USERPROFILE% at all. I guess it can just be $HOME as a wiki link and in that page a Win32 section explains it. Not sure if I'm more disappointed in Windows or Vim on this one. --JeremyBarton 16:49, 30 September 2008 (UTC)
Problems, problems... I'm processing the proposed new tips for September. Apart from redirects, every page in our Main namespace is a tip, and has one of these templates: TipImported, TipNew, TipProposed. I'm not going to put TipProposed on this (has an ugly box). Probably there should be no template and this should just be a normal Wikipedia-style page. However, I'm not ready to make decisions like that because I'm still in frantic "must clean up the old tips" mode. Also, it's really handy having simple tip numbers to refer to pages while doing lots of maintenance work. So, I've put TipNew with a dummy tip number and will think about it later. Let's delete my comment in a week or so, when everyone has had a chance to see it. --JohnBeckett 00:40, 1 October 2008 (UTC)