JohnBeckett (talk | contribs) (Standardise with TipNew) |
(→Comments: why not use tip templates?) |
||
Line 119: | Line 119: | ||
---- |
---- |
||
+ | |||
+ | I personally ''like'' the tip numbers (don't really have a reason why though, I never use them), so I see no reason ''not'' to use TipNew, TipProposed, etc. I'm not sure why this tip isn't going through the usual process, though I suppose there isn't much chance of it ''not'' being accepted. Perhaps you are saying that this page isn't really a "tip" so much as a reference page? If we want to start doing that, there are quite a few other tips (that mass add back in February comes to mind, as well as the "mapping keys" tutorials) that the same sort of thing might apply to...but how should we handle that? |
||
+ | |||
+ | --[[User:Fritzophrenic|Fritzophrenic]] 14:01, 1 October 2008 (UTC) |
Revision as of 14:01, 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)
I personally like the tip numbers (don't really have a reason why though, I never use them), so I see no reason not to use TipNew, TipProposed, etc. I'm not sure why this tip isn't going through the usual process, though I suppose there isn't much chance of it not being accepted. Perhaps you are saying that this page isn't really a "tip" so much as a reference page? If we want to start doing that, there are quite a few other tips (that mass add back in February comes to mind, as well as the "mapping keys" tutorials) that the same sort of thing might apply to...but how should we handle that?
--Fritzophrenic 14:01, 1 October 2008 (UTC)