Disable syntax highlighting for certain filenames

强颜欢笑 提交于 2019-12-11 02:55:15

问题


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!


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/27440400/disable-syntax-highlighting-for-certain-filenames

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!