Friday, June 18, 2010

Re: set filetype in .vimrc

On 2010-06-18, aleCodd wrote:
> Tony Mechelynck-2 wrote:
> >
> > Most Vim helpfiles have a modeline at the end, something like
> >
> > vim:tw=78:ts=8:ft=help:norl:
> >
> > (see :help modeline)
> >
> > The :ft=help: part in such a modeline (intentionally) overrides any
> > filetype which might have been set by the filetype-detection mechanism.
> > Normally you wouldn't want to override such modelines, they're there for
> > good reason. (For instance I have an own-coded rule to identify *.txt
> > files as filetype "text", but thanks to these modelines it doesn't apply
> > to the Vim help.)
> >
> >
>
> I removed the modeline, and still defaults back to help filetype, and the
> weird thing is that if i remove the 'syntax enable' and 'filetype on'
> commands from the .vimrc the filetype would be to whatever i manually set in
> the autocommand, but these SAME two commands don't overrule a buffer
> filetype after it has been already set.

If you want to find out what is setting 'ft', execute this:

verbose set ft?

> does 'filetype on' command behave differently from the .vimrc vs. the
> command line?

The command itself behaves the same regardless of where it is
executed. The effect of executing it may vary depending on when it
is executed relative to other commands that define autocommands.

Executing ":filetype on" loads the file $VIMRUNTIME/filetype.vim
(see ":help :filetype") and that file executes a whole lot of
autocommands that respond to the BufNewFile and BufRead events and
execute the :setf command in response. Conceptually, Vim maintains
a list of autocommands for each autocommand event. When an event
occurs, Vim traverses the list of autocommands for that event and
executes any whose patterns match. The autocommands are executed in
the same order as they are added to the list.

What that means is that if you execute an autocommand, e.g.,

au BufRead *.c setf python

and then execute

filetype on

your autocommand will precede those loaded by ":filetype on" and
will be executed first whenever a file ending in ".c" is read.

In the case of the :setf command, the first one executed in response
to a single event wins. Following my example, executing

:e foo.c

where foo.c already exists will result in 'filetype' being set to
"python".

Now, if instead you executed those commands in this sequence,

:filetype on
:au BufRead *.c setf python
:e foo.c

'filetype' would be set to "c".

If you used "set filetype=python" instead of "setf python", like
this,

:filetype on
:au BufRead *.c set filetype=python
:e foo.c

then 'filetype' would be set to "python" because "set
filetype=python" is executed last and because the :set command
forces an option to be set unconditionally whereas :setf sets the
'filetype' conditionally as described in

:help :setf

That's a lot of information. The points are that:

1. ":filetype on" defines a set of autocommands;
2. autocommands are executed in the order in which they are
defined;
3. :setf sets the filetype conditionally (the first one in a
sequence wins);
4. ":set ft=" sets the filetype unconditionally (the last one in a
sequence wins); and
5. (not discussed above) modelines are executed after any BufRead
autocommands.

I hope understanding that will help explain what you've been
observing.

Regards,
Gary

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: