Disable syntax highlighting for certain filenames

3k views Asked by At

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!

3

There are 3 answers

3
Ingo Karkat On BEST ANSWER

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
5
Ashwani On

If you want to show syntax only for .c files. Put

syntax off
autocmd! bufreadpost *.c syntax on

in your vimrc.

Also you can map a key for enabling syntax (Ctrl+s in this case)

nnoremap <C-S> :syntax on<CR>

In you question you want to disable syntax only for .inc file. Do it like this:

syntax on
autocmd! bufreadpost *.inc set syntax=off
0
Isin Altinkaya On

To disable syntax highlighting for files with .inc extension, you can basically use:

syntax on
au BufNewFile,BufRead *.inc setlocal syntax=OFF

To disable it for multiple extensions, e.g. also for py:

au BufNewFile,BufRead *.{inc,py} setlocal syntax=OFF