views:

225

answers:

2

I have a file, called WrongFileTypeDetection.R, containing the following text:

# This file is sometimes wrongly detected as a conf file

I also have two versions of _vimrc, that I would have thought to be absolutely identical. However, when I use the first version, the above file is incorrectly detected as a "conf" file, even though I've specifically asked for all files ending with .R to be set to filetype=r. When I change to the second version (moving "syntax on" behind the augroup definition), detection works correctly again. Note that this is the only configuration I have (I moved away my standard vimrc while debugging this).

First version:

syntax on
augroup filetypedetect
    autocmd! BufRead,BufNewFile *.r,*.R  setfiletype r
augroup END

Second version:

augroup filetypedetect
    autocmd! BufRead,BufNewFile *.r,*.R  setfiletype r
augroup END
syntax on

It seems that vimrc is very sensitive to the particular ordering of the two files. Why would this be, considering that one of the lines is an autocommand that will be run much later anyway? Is this a bug in Vim or is this a feature that I'm just not understanding?

+5  A: 

In your first version, syntax on is triggering the filetype detection before your autocommand has been added.

too much php
+1  A: 

Thanks for the answer. I thought something like that was going on. However, I guess the follow-up question is, why does it matter when the autocommand is added. I would have thought it only matters when it is executed.

Consider the following (where vimrc contains the original text lines):

$ vim
(wait five seconds)
:e WrongFileTypeDetection.R

I still get the same issue, although the filetype detection can clearly not have been triggered until the file was opened - an eternity (in cpu-time) after the autocommand was added.

This is not purely of academic interest for me. I would like to understand the process as well as possible, because at another installation (Vim 6), the simple solution of switching the order of these two statements is not fixing the problem.

I'd be happy to read a manual, if anyone could point me to a location where these kinds of race conditions are explained in detail.