Search code examples
syntax-highlightingvimvim-syntax-highlighting

How does .vimrc set its own filetype?


In response to this question on SuperUser, I wrote a small vimscript that will detect the filetype of a symbolic link and change the syntax highlighting:

au BufNewFile,BufRead * if &syntax == '' | silent! execute (':set filetype='.matchstr(resolve(@%),'.[^.]*$')[1:]) | endif

So if I open a symbolic link with no extension, it will look at the extension of the file it points to.

It works, but an unintended consequence is that now the syntax highlighting of my .vimrc file is gone. By default, my .vimrc file has vim syntax highlighting (:echo &syntax returns vim).

But when I add the above line, :echo &syntax returns vimrc (an invalid type).

I don't know why this is happening. Shouldn't &syntax=='' evaluate to false, and thus keep &syntax==vim? I suspect that my code is executing before the syntax highlighting is set to vim. But how (and when) exactly is the syntax highlighting set to vim for .vimrc? Additionally, how can I make my script behave the way it should?


Solution

  • Look in Vim's runtime area for filetype.vim. You can bring it up in vim with:

    :e $VIMRUNTIME/filetype.vim
    

    In my version, it looks like this line does the trick:

    " Vim script
    au BufNewFile,BufRead *vimrc*           call s:StarSetf('vim')
    

    Perhaps you want to put your autocmd in ~/.vim/after/filetype.vim. I believe this will cause it to be registered after the system ones, and then &syntax should be set up correctly.