vimsyntax-highlightingautocmd

Disable syntax highlighting for certain filenames


I have syntax highlighting enabled in .vimrc, but that makes loading certain files too long. So I need to disable (or, to be precise, not enable... enabling it and then disabling is not a solution) syntax highlighting for these files. I tried

au BufNewFile,BufRead !*.inc syntax enable

but that made just no syntax highlighting applied ever. The solution presented here does not work for me, since I can't make a distinction by filetype. I tried adapting to no avail, which might or might not be connected to the events needed for "syntax enable".

Thanks for any pointers!


Solution

  • The mentioned solution points to the right direction: Define an autocmd for all buffers, and then (instead of 'filetype'), match with the filename via expand('<afile>'):

    au BufNewFile,BufRead * if expand('<afile>:e') !=? 'inc' | syntax enable | endif
    

    Here, I've used your example of *.inc extensions in the condition. If you find the matching cumbersome and would rather use the autocmd syntax, you can do that with an intermediate buffer flag, too, using the fact that autocmds are executed in order of definition:

    au BufNewFile,BufRead *.inc let b:isOmitSyntax = 1
    au BufNewFile,BufRead *     if ! exists('b:isOmitSyntax') | syntax enable | endif