Max Dyckhoff wrote:
Thank you for your answers.
See ":help ftdetect". The "augroup" statements are also not necessary.
The variable "did_load_filetypes" is set to 1 by
$VIMRUNTIME/filetype.vim (at line 10 in the version for Vim 7.0, lat
change May 02). Your script is sourced from line 2166 of the same
file,
i.e., much later.
That was what I was thinking. How does one get "C. If your file type can
be detected by the file name." to work (from :help new-filetype) then,
given that my file type can in fact be detected by the filename.
It's not a bug, it's a feature.
As mentioned under ":help ftdetect" (in the NOTE at line 178 of
filetype.txt for Vim 7.0, last change 2006 Apr 28), your
$VIM/ftdetect/wibble.vim (or, maybe better, wibblewobble.vim) should
only contain the single line
Deliberately named wibble.vim; this is just a test and I wanted to make
sure it wasn't just taking the filetype from the file suffix.
Method 1: Like you did.
---------
Create a one-line file named one of
~/.vim/ftdetect/wibblewobble.vim (user-specific on Unix including Linux
and Cygwin)
~/vimfiles/ftdetect/wibblewobble.vim (user-specific on Windows, but not
on Vim for Cygwin)
$VIM/vimfiles/ftdetect/wibblewobble.vim (system-wide on any platform)
and containg just
au BufRead,BufNewFile *.wibble setlocal ft=wibblewobble
(the filename is unimportant but making the same as the filetype avoids
clashes with other future "nonstandard" filetypes you may want to add,
and it allow detection by more than one extension. For instance,
replacing the line above by
au BufRead,BufNewFile *.wibble,*.wobble setlocal ft-wibblewobble
would use the filetype "wibblewobble" for all files named either
*.wibble or *.wobble. The directory where the file is placed _is_
important. If the directory you want to use doesn't exist, just create
it using mkdir in the shell or :!mkdir in Vim. (On Windows, but not on
Unix, md can of course be used as a synonym for mkdir.)
Method 2: One centralized ft-detection script.
---------
This may be more or less advantageos than Method 1 depending on
circumstances. Here both the name of the file and its directory (one of
three) are rigidly fixed. Again, if the file and/or its directory don't
exist, you may create them. Here are the applicable pathfilenames:
~/.vim/filetype.vim single-user on Unix
~/vimfiles/filetype.vim single-user on Windows
$VIM/vimfiles/filetype.vim system-wide on all platforms
That file consistes of a header, a trailer, and one line per
"user-defined" filetype in-between. It may also contain any number of
comments (a comment in vim-script language is defined as anything
starting with a double-quote and ending at the end of the line. There
are a few ex-commands which may not be followed by a comment but most of
them, including ":set" and friends, may. Blank lines are also allowed:
their only role is to improve legibility.
" --- Header part --- {{{1
" Filetype-detection script for user-defined filetypes
" Written by: <your name here>
" Last change: 13 Jun 2006
" Don't run this twice
if exists("b:did_load_filetypes)
finish
endif
" We do NOT set did_load_filetypes here
" because the global filetype.vim must not be inhibited.
augroup filetypedetect
" --- Middle part: list of autocommands --- {{{1
au BufRead,BufNewFile *.wibble,*.wobble setf wibblewobble
au BufRead,BufNewFile *.foo,*.bar,*.baz setf foobarbaz
au BufRead,BufNewFile *.foobar setf foobaronly
" etc...
" --- Trailer part --- {{{1
augroup END
" vim: textwidth=0:foldmethod=marker
The |modeline| at the end and the {{{ folding-markers |fold.txt| are
optional but I recommend using them.
Method 3: Combine both of the above.
---------
It is also possible to detect some filetypes by mleans of a central
filetype.vim and others by individual ftdetect/*.vim scripts. IMHO the
only reason for this would be if you wrote some of the filetype-plugins
yourself (and detected their respective filetypes centrally), and
downloaded ftplugin/<filetype>.vim, syntax/<filetype>.vim,
indent/<filetype>.vim and ftdetect/<filetype>.vim from somewhere on the
Web for other filetypes.
Also, it is possible to detect some filetypes system-wide and others
user-privately: the filetype-detection scripts are run in the following
sequence (if one of them is not found, the result is the same as if it
were present but did nothing; where I use a wild card, _all_ scripts
verifying the wildcarded name will be run):
~/.vim/filetype.vim (on Unix) or ~/vimfiles/filetype.vim (on Windows)
$VIM/vimfiles/filetype.vim
$VIMRUNTIME/filetype.vim
~/.vim/ftdetect/*.vim (on Unix) or ~/vimfiles/ftdetect/*.vim (on Windows)
$VIM/vimfiles/ftdetect/*.vim
$VIMRUNTIME/ftdetect/*.vim (which currently doesn't exist but in theory
it could).
IMPORTANT: Never change anything in $VIMRUNTIME or in any of its
subdirectories. The files there are downloaded when installing Vim; any
upgrade may change any of them without warning, and upgrading to a new
release such as 7.1 or 8.0 _will_ recreate it all in a directory whose
path will end in /vim71/ or /vim80/ instead of /vim70/. OTOH, $HOME and
$VIM won't change even across releases.
Then you will probably want to write one or more of the following
scripts (else the filetype detection is rather pointless):
Again this was just a test to see how things worked, so I don't really
need any of the other files at this point, but thanks!
Max
Best regards,
Tony.