I often have to edit various XML files with vim, which vary wildly in size - from configuration files containing a few hunded lines to production data files with sizes up to 2GB. Having syntax highlighting enabled is of course a very bad idea when dealing with huge files, thus I want to disable it if the file is bigger than a threshold.
I could not get this to work using
autocommand directly to disable syntax highlighting, as apparently the command is executed before syntax is enabled when starting vim from the shell:
" this autocmd has no effect except for the echo: autocmd Filetype xml if getfsize(@%) > 1000000 | echom '!' | syntax off | endif
I found a workaround in that I can disable syntax highlighting globally, then turn it on again for all other filetypes than xml, and turn it on for filetype xml if the file is not bigger than the threshold:
syntax off autocmd Filetype * syntax off autocmd Filetype * if &ft != 'xml' | syntax enable | endif autocmd Filetype xml if getfsize(@%) < 1000000 | syntax enable | endif
This seems to work, but feels wrong and will become unmanageable once I want to do this for more filetypes and conditions. Furthermore, it influences all buffers. What is the proper way to disable syntax highlighting in one buffer under specific conditions?