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?
The simple answer to your problem is that these lines shouldn't be in your vimrc at all. The officially supported way to handle this is create a filetype.vim
file in your .vim
directory containing the following:
" my filetype file
if exists("did_load_filetypes")
finish
endif
augroup filetypedetect
au! BufRead,BufNewFile *.r setfiletype r
augroup END
As for why your code is behaving how it does, it's actually quite a bit more complicated than @too much php's answer implies.
In the first version of your vimrc
, filetype detection is initialised by your syntax on
line. This works by setting up an autocmd to fire when a file with a .r
extension is opened, and this autocmd calls the function s:FTr()
in filetype.vim.
However, your autocmd!
line overwrites this existing autocmd1, so the s:FTr()
function never runs2. Your autocmd then fires, but it doesn't set the filetype, because the setfiletype
command thinks that the filetype has already been set3.
Then, because the filetype is still not actually set, Vim attempts to assign one based on the contents of the file, and ends up assigning it to the conf
type4.
The best reference for all this is :help filetype
. And specifically in your case :help new-filetype
and :help remove-filetype
. The :verbose
command is also very handy for figuring out which settings have been set by what script.
1: Because of the !
. If you were to remove this !
, then you would find the filetype being set correctly. This is not the correct solution, though, because then you will be allowing the file to be set to one type (and applying all the settings for that filetype) and then changing it to another. If the second filetype doesn't overwrite all these settings, some may remain, which might not be what you want.
2: Try running :set autocmd BufRead *.r
when you are using the two different vimrc files. Note the difference in the output given.
3: See :help setfiletype
. Note that if you change the line setfiletype r
into set ft=r
in the first version of your vimrc, the filetype will be set to r. However, see footnote 1 for why this is not the best solution.
4: ...in a line of code which is commented: "check this last, it's just guessing"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With